/*        For System Pack V1.2 or above      */

/*
************************************
*       Module : system.h          *
*       Name   : TF                *
*       Date   : 27-02-2009        *
************************************
12-08-2010
1. Add USB host interface
2. Add Device driver interface
3. Add Error Code for device
4. Add os_file_ioctl

01-04-2010
1. Add tilt sensor interface
2. Add PN532 interface
3. Update hardware config
4. Add Tilt parameter in config
5. change RC531 to PN532

25-8-2009
1. add sms enable config
2. add sms send request and sms send error flag in gprs_status
3. add gprs_sms_control interface
*/

#ifndef _SYSTEM_H_
#define _SYSTEM_H_
#include "common.h"

#ifndef _UTIL_
#include "app_info.h"
#include "at91sam9261.h"
#endif

#define AT91C_MBASE_LCDC      ((AT91PS_LCDC)  0xFFF9F000) // (LCDC) Base Address
#define K_LanBase             0xFFF9E000
#define K_SrLptReg            0xFFF99000
#define K_PwrCtrlReg          0xFFF99004
#define K_EthernetPowerb      0
#define K_Rs232Power          1
#define K_MsrPower            2
#define K_KbdBackLight        3
#define K_EthernetResetb      4
#define K_MdmResetb           4

extern DWORD os_call_app(DWORD a_appid,DWORD a_param1,DWORD a_param2,DWORD a_param3);
extern DWORD lib_app_call(DWORD a_appid,DWORD a_func_no,DWORD a_param1,DWORD a_param2, DWORD a_param3);
extern __pcs DWORD sys_func0(DWORD a_func_no);
extern __pcs DWORD sys_func1(DWORD a_func_no,DWORD a_param1);
extern __pcs DWORD sys_func2(DWORD a_func_no,DWORD a_param1,DWORD a_param2);
extern __pcs DWORD sys_func3(DWORD a_func_no,DWORD a_param1,DWORD a_param2,DWORD a_param3);

#define K_SysProc               0

// application ID
#define K_SysID                 0
#define K_ProdCode              1
#define K_DriverID              2
#define K_BaseID                3
#define K_Font                  4
#define K_FontSp                5
#define K_SamDriver             6
#define K_BtLoader              8
#define K_MfgID                 9
#define K_InternalID            10
#define K_EmvDllID              14
#define K_MidID                 15
#define K_ApmID                 16
#define K_UserID                17

#define K_MaxProcAllowed        100             // pid 1 to 100

// GPRS reqest ID used by system only
#define K_GprsCmuxTimeout       0x80
#define K_GprsCmuxSmsReq        114
#define K_GprsCmuxData          113
#define K_GprsCmuxWakeResp      112
#define K_GprsCmuxDLC0          111
#define K_GprsCmuxCmd           110
#define K_GprsCmuxSdlcCmd       109
#define K_GprsCmuxWakeReq       108
#define K_GprsCmuxMscDtrOn      107
#define K_GprsCmuxMscDtrOff     106
#define K_GprsCmuxBattVolt      105
#define K_GprsCmuxBattTemp      104
#define K_GprsCmuxPHeadTemp     103
#define K_GprsCmuxSmsResult     102
#define K_GprsReset             101

// Product Code service
#define K_ProductDescription    0
#define K_HwConfig              1

// font file interface
#define K_UFontScreenWidth      26              /* Max 26 col */
#define K_UFontScreenHeight     9               /* Max 9 row */

typedef struct {
  BYTE s_bitmap[32];                            /* up to 16x16 dots */
  WORD w_status;
  WORD w_temp;
} T_USER_FONT;

//User Font return bit
#define K_UFontHeight8          0x00
#define K_UFontHeight16         0x01
#define K_UFontWidth8           0x00
#define K_UFontWidth16          0x02
#define K_UFont8x8              0x00
#define K_UFont8x16             0x01
#define K_UFont16x8             0x02
#define K_UFont16x16            0x03
#define K_UFont6x8              0x04
#define K_UFont12x16            0x05
#define K_UFontReady            0x80

typedef struct {
  WORD  w_background;
  WORD  w_battery_frame;
  WORD  w_battery_level;
  WORD  w_anntena;
  WORD  w_signal;
  WORD  w_tx_only;
  WORD  w_rx_only;
  WORD  w_tx_rx;
} T_DISP_ICON_COLOR;

// RTC
typedef struct {
  BYTE  s_century[2];
  BYTE  s_year[2];
  BYTE  s_month[2];
  BYTE  s_day[2];
  BYTE  s_hour[2];
  BYTE  s_min[2];
  BYTE  s_sec[2];
} T_DTG;

// SSL
typedef struct {
  DWORD s_total[2];
  DWORD s_state[4];
  BYTE  s_buffer[64];
} T_MD5_CONTEXT;

#define T_MD4_CONTEXT           T_MD5_CONTEXT

typedef struct {
  DWORD s_total[2];
  DWORD s_state[5];
  BYTE  s_buffer[64];
} T_SHA1_CONTEXT;

typedef struct {
  DWORD d_x, d_y, s_m[256];
} T_RC4_STATE;

// app header
typedef struct {
  DWORD p_signature;
  DWORD p_fntbl;
  DWORD p_virgin;
  DWORD p_appname;
  BYTE  b_version;
  BYTE  b_subver;
  BYTE  b_type;
  BYTE  b_appid;
  DWORD d_magic;
} T_APP_HDR;

// MMC
typedef struct {
  DWORD d_block_no;
  DWORD d_offset;
  BYTE *p_blk;
} T_MMC;

// ICC T0 & T1
typedef struct {
  BYTE    b_output;
  BYTE    b_class;
  BYTE    b_command;
  BYTE    b_p1;
  BYTE    b_p2;
  BYTE    b_p3;
  WORD    w_response;
  BYTE *  p_mblk;
} T_T0;

typedef struct {
  BYTE *  p_sad_buf;
  WORD    w_sad_len;
  BYTE    b_sad;
  BYTE    b_rfu1;
  BYTE *  p_dad_buf;
  WORD    w_dad_len;
  BYTE    b_dad;
  BYTE    b_rfu2;
} T_T1;

            /****** RSA define ******/
#define K_MaxRsaModulusBits     2048
#define K_MinRsaModulusBits     1024

// RSA public key define for EMV L2
typedef struct {
  DWORD  d_exponent;
  DWORD  d_keysize;
  DWORD  s_key[K_MaxRsaModulusBits/32];
  DWORD  d_key_idx;
} T_KEY;

// RSA private key lengths
#define K_MaxRsaModulusLen      ((K_MaxRsaModulusBits + 7) / 8)
#define K_MaxRsaPrimeBits      ((K_MaxRsaModulusBits + 1) / 2)
#define K_MaxRsaPrimeLen       ((K_MaxRsaPrimeBits + 7) / 8)

typedef struct {
  DWORD d_key_idx;
  DWORD d_bits;                                 /* length in bits of modulus */
  BYTE s_modulus[K_MaxRsaModulusLen];           /* modulus */
  BYTE s_prime[2][K_MaxRsaPrimeLen];            /* prime factors */
  BYTE s_prime_exponent[2][K_MaxRsaPrimeLen];   /* exponents for CRT */
  BYTE s_coefficient[K_MaxRsaPrimeLen];         /* CRT coefficient */
} T_RSA_PRIVATE_KEY;

            /****** application header define ******/

typedef struct {
  WORD  w_checksum;
  WORD  w_disp_cs;
  BYTE  s_agent_id[8];
  BYTE  s_app_name[13];
  BYTE  b_version;
  BYTE  b_subver;
  BYTE  b_type;
  BYTE  b_appid;
} T_APP_INFO;

#define K_DllMask               0x01    // 0 = normal app, 1 = dll
#define K_Normal                0x00
#define K_Dll                   0x01

#define K_SignMask              0x06    // 00 = no sign
                                        // 01 = user sign
                                        // 10 = app sign
                                        // 11 = system sign
#define K_NoSign                0x00
#define K_UserSign              0x02
#define K_AppSign               0x04
#define K_SysSign               0x06

#define K_DevMask               0x08    // 0 = normal, 1 = device driver
#define K_DevDrv                0x08

                /****** rdll application interface structure define ******/
#define K_RdllResultError       0xff
#define K_RdllResultOK          0
#define K_RdllResultProcessing  1

#define K_RdllReqTypeMask       0x0F  // request type mask
#define K_RdllSysRequest        0x00
#define K_RdllAppRequest        0x01
#define K_RdllContMask          0xF0  // request control bits mask
#define K_RdllFlush             0x80

#define K_RdllPppGprs           0
#define K_RdllEthernet          1
#define K_RdllPppModem          2
#define K_RdllMax               (K_RdllPppModem+1)

#ifdef __IAR_SYSTEMS_ICC__
#pragma pack(1)
#endif
typedef struct {
  BYTE  b_link_type;      // 0 = GPRS; 1 = Ethernet; 2 = modem PPP; 3 = modem
  BYTE  b_result;
  BYTE  b_d_type;         // bit 0 : will be filled by system
                          //   0 = system request; 1 = user app request
                          // bit 7 : filled by caller
                          //   0 = no delete temporary files before rdll start
                          //   1 = delete all temporary files before rdll start
  BYTE  b_app_type;
  BYTE  s_app_id[32];
  BYTE  s_phone1[35];
  BYTE  s_phone2[35];
  WORD  w_fport;          // in little endian
  DWORD d_fip;            // in big endian
  BYTE  s_ppp_user[16];
  BYTE  s_ppp_psw[16];
  BYTE  s_gprs_apn[32];
// currently not support at this moment
  BYTE  s_ftp_user[40];
  BYTE  s_ftp_psw[20];
  BYTE  s_ftp_dir[88];
  BYTE  s_wifi_ssid[32];
  BYTE  b_wifi_auth;
  BYTE  b_wifi_encr;
  BYTE  b_wifi_keyidx;
  BYTE  b_wifi_hex;
  BYTE  s_wifi_key[64];
  BYTE  s_rfu[200];
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
T_RDLL_INF;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

                /****** aux & mcom common define ******/

#define K_LinkStatus            0
//- error will be reset after reported and lower byte is the error
#define K_LinkTxEmpty           0x010000
#define K_LinkExist             0x008000
#define K_LinkOnLine            0x004000
#define K_LinkTxRdy             0x002000
#define K_LinkRxRdy             0x001000
#define K_LinkTxError           0x000800
#define K_LinkRxError           0x000400
#define K_LinkIntError          0x000200
#define K_LinkRxOverflow        0x000100

                /****** bufman define ******/
// used in manbuf alloc
#define K_SingleBuf             0
#define K_DoubleBuf             1
#define K_QuadBuf               2

// actual status stored in buffer status
#define K_BufFree               0
#define K_BufSingle             1
#define K_BufDouble             2
#define K_BufQuad               3

#define K_MaxBuf                150
#define K_BufHdrLen             9
#define K_BufSize               760
#define K_BufOffset             64
#define K_TBufSize              (K_BufSize+K_BufOffset)
#define K_AppendSize            8
#define K_HeadSize              32
#define K_RfuSize               (K_HeadSize-K_BufHdrLen)
#define K_T2BufSize             (K_TBufSize*2+K_HeadSize+K_AppendSize)
#define K_T4BufSize             (K_TBufSize*4+(K_HeadSize+K_AppendSize)*3)
#define K_2BufSize              (K_T2BufSize-K_BufOffset)
#define K_4BufSize              (K_T4BufSize-K_BufOffset)

typedef struct {
  BYTE  b_status;
  BYTE  b_type;
  WORD  w_opt;
  WORD  w_offset;
  WORD  w_len;
  BYTE  b_cmux_pid;             // only in gprs cmux only
  BYTE  rfu[K_RfuSize];
  BYTE  s_buf[K_TBufSize+K_AppendSize];
} T_BUF;

/* opt define */
#define K_PppData               0x0001

                /****** keydes DUKPT structure ******/

#define K_Dukpt                 0xFF
#define K_NotInjected           0xF1

#define K_NbrBaseKey            21

typedef struct {
  DWORD  d_good;
  BYTE   s_key[16];
} T_BASEKEY;

typedef struct {
  T_BASEKEY s_basekey[K_NbrBaseKey];
  DWORD     d_ECounter;
  DWORD     d_ShiftReg;
  DWORD     d_FutureKeyIdx;
  BYTE      s_KSN[10];
  WORD      w_crc;
} T_KEYBANK;

                /****** keydes mode ******/
#define K_SingleKeyLenDes       0
#define K_DoubleKeyLenDes       1
#define K_TripleKeyLenDes       2
#define K_PublicRsa             3
#define K_PrivateRsa            4

                /****** device driver define ******/
#define K_MaxDevice             10

#define K_SwitchSystem          0
#define K_SwitchUser            1

// driver
#define K_SdDevice              0
#define K_MsrDevice             1
#define K_IccDevice             2
#define K_LanDevice             3
#define K_LptDevice             4
#define K_TiltDevice            5
#define K_PN532Device           6

// lan driver functions
#define K_LanFOpen              0
#define K_LanFClose             1
#define K_LanFRxRdy             2
#define K_LanFTxRdy             3
#define K_LanFRead              4
#define K_LanFSend              5
#define K_LanFStatus            6
#define K_LanFStatistic         7
#define K_LanFCheckInstalled    8
#define K_LanFHandler           9
#define K_LanFEnabled           10

// Sam driver functions
#define K_SamFOpen              0
#define K_SamFClose             1
#define K_SamFOn                2
#define K_SamFT1                3
#define K_SamFSelect            4
#define K_SamFOff               5
#define K_SamFChanged           6
#define K_SamFInserted          7
#define K_SamFActive            8

// SD driver functions
#define K_SdFOpen               0
#define K_SdFClose              1
#define K_SdFBlkSize            2
#define K_SdFSize               3
#define K_SdFBlkRead            4
#define K_SdFBlkWrite           5
#define K_SdFRead               6
#define K_SdFWrite              7
#define K_SdFPStatus            8
#define K_SdFHandler            9
#define K_SdFStart              10
#define K_SdFInserted           11
#define K_SdFEnabled            12

// msr driver functions
#define K_MsrFOpen              0
#define K_MsrFClose             1
#define K_MsrFRxLen             2
#define K_MsrFGetc              3
#define K_MsrFRead              4
#define K_MsrFStatus            5
#define K_MsrFTrk2Handler       6
#define K_MsrFTrk1Handler       7
#define K_MsrFTrk3Handler       8
#define K_MsrFTrk2Full          9
#define K_MsrFTrk1Full          10
#define K_MsrFTrk3Full          11
#define K_MsrFTrackActive       12
#define K_MsrFEnabled           13

// memory ICC driver functions
#define K_IccFOpen              0
#define K_IccFClose             1
#define K_IccFIO                2
#define K_IccFHist              3
#define K_IccFEnabled           4

// lpt driver functions
#define K_LptFOpen              0
#define K_LptFClose             1
#define K_LptFPutc              2
#define K_LptFPutdw             3
#define K_LptFStatus            4
#define K_LptFTempMonitor       5
#define K_LptFHandler           6
#define K_LptFShiftFinish       7
#define K_LptFBusy              8
#define K_LptFCheckInstalled    9
#define K_LptFEnabled           10

// tilt driver functions
#define K_TiltFOpen             0
#define K_TiltFClose            1
#define K_TiltFRead             2
#define K_TiltFCheckInstalled   3

// PN532 driver functions
#define K_PN532FOpen             0
#define K_PN532FClose            1
#define K_PN532FPollCard         2
#define K_PN532FSelect           3
#define K_PN532FDeselect         4
#define K_PN532FRelease          5
#define K_PN532FPsl              6
#define K_PN532FSend             7
#define K_PN532FSendRaw          8
#define K_PN532FSendDirect       9
#define K_PN532FResponse         10
#define K_PN532FCancelCmd        11
#define K_PN532FConfigSet        12
#define K_PN532FConfigGet        13
#define K_PN532FReadReg          14
#define K_PN532FWriteReg         15
#define K_PN532FInit             16

// system function for device driver
enum {
K_SysDisableInterrupt,
K_SysEnableInterrupt,
K_SysDtimerStop,
K_SysDtimerCheckExpired,
K_SysDtimerWaitExpired,
K_SysDtimerWatchSet,
K_SysDtimerWatchWait,
K_SysDtimerWatchCheck,
K_SysInstall10ms,
K_SysUninstall10ms,
K_SysDispBlControl,
K_SysMsrTrackActive,
K_SysSpi0RDequeue,
K_SysSpi0HandleFree,
K_SysSpi0NextTask,
K_SysSpi0HandleAlloc,
K_SysSpi0NextSeq,
K_SysSpi0Send,
K_SysSpi0REnqueue,
K_SysSpi0GetPtr,
K_SysAddrAllow,
K_SysInstall5ms,
K_SysUninstall5ms,
K_SysSamActive,
K_SysSwitchMode,
K_SysVM2PhyAllocAddr,
K_SysPhy2VMAllocAddr,
K_SysSwitchContext,
K_SysLanInstalled,
K_SysBufmanAlloc,
K_SysBufmanFree,
K_SysWdtReset,
K_SysGprsDerq,
K_SysGprsAtICmd,
K_SysGprsReleaseBuffer,
K_SysGprsStatus,
K_SysBattInstantLevel,
K_SysFontGet,
K_SysFontUserGet,
K_SysFontInfoGet,
K_SysPowerControl,
K_SysBattLevel,
K_SysSpi0PN532Control,
K_SysSpi0PN532Waken,
K_SysSpi0PN532Cancel,
K_SysAdcEnable,
K_SysAdcDisable,
K_SysAdcRead,
K_SysAdcSuspend,
K_SysGetContext,
K_SysEnd
};

typedef struct {
  DWORD p_main;
  DWORD p_name;
  BYTE  b_version;
  BYTE  b_subver;
  BYTE  b_devid;
} T_DEV_HDR;

                /****** Tilt Sensor define ******/

typedef struct {
  float d_x;
  float d_y;
  signed char  b_x_deg;
  signed char  b_y_deg;
  BYTE  rfu[2];
} T_TILT;

                /****** system hardware define ******/

#define MCLK                    (57139200*2)  //(11059200/3*62)/2
// cache define
#define K_CacheL                5
#define K_CacheLineLen          (1<<K_CacheL)

#define K_Pit5MS                (MCLK/16*5/1000)
#define K_T1US                  (MCLK/8/1000000 + 1)
#define K_T2US                  (MCLK/8/500000 + 1)
#define K_T5US                  (MCLK/8/200000 + 1)
#define K_T10US                 (MCLK/8/100000 + 1)
#define K_T20US                 (MCLK/8/50000 + 1)
#define K_T100US                (MCLK/8/10000 + 1)
#define K_T1MS                  10
#define K_T5MS                  50
#define K_T8MS                  80
#define K_T10MS                 100

// Port redefine
#define PIOA_BASE               AT91C_BASE_PIOA
#define PIOB_BASE               AT91C_BASE_PIOB
#define PIOC_BASE               AT91C_BASE_PIOC

#define pin_on(x,y)             ((x)->PIO_SODR = y)
#define pin_off(x,y)            ((x)->PIO_CODR = y)
#define pin_in(x,y)             ((x)->PIO_PDSR & (y))

#ifndef _UTIL_
// pin structure
typedef struct {
  AT91PS_PIO p_port_base;
  DWORD d_pos;
} T_PIN;
#endif

// SPI0 command structure
#define K_SPI0Mask          0x07
#define K_SPI0Lpt           0
#define K_SPI0LptDummy      0x80
#define K_SPI0Lcd           1
#define K_SPI0PN532         2
  #define K_SPI0PN532Raw    0x80    // indicate from os_pn532_send_raw()
  #define K_SPIOPN532Direct 0x40    // inidcate from os_pn532_send_direct()
  #define K_SPI0PN532Cancel 0x20    // indicate this command is cancelled
#define K_SPI0MMC           3
#define K_SPI0Adc           4
#define K_SPI0Empty         0xff

typedef struct {
  BYTE    b_idx;
  BYTE    b_state;
  WORD    w_seq;      // for caller use only
  DWORD   d_timeout;
  union {
    struct {
      BYTE *  p_cmd;
      BYTE *  p_sbuf;
      BYTE *  p_rbuf;
      WORD    w_slen;
      WORD    w_rlen;
      BYTE    b_clen;
    };
    struct {
      DWORD   d_cmd;
      DWORD   d_arg;
    };
  };
} T_SPI0;

                /****** mdm define ******/

#define K_ConnectSpeed300       0
#define K_ConnectSpeed1200      1
#define K_ConnectSpeed2400      2
#define K_ConnectSpeed9600      3
#define K_ConnectSpeedAuto      4
#define K_ConnectSpeedAutoc     5
#define K_ConnectSpeedAutocu    6
#define K_ConnectSpeedMax14400  7
#define K_ConnectSpeedMax33600  8
#define K_ConnectSpeedMax57600  9
#define K_ConnectUnionpaySupp   0x80

#define K_Albania               0x01
#define K_Algeria               0x02
#define K_Andorra               0xE0
#define K_Angola                0x43
#define K_Anguilla              0x05
#define K_AntiguaBarbuda        0x06
#define K_Argentina             0x07
#define K_Aruba                 0xE1
#define K_Ascension             0x08
#define K_Australia             0x09
#define K_Austria               0x0A
#define K_Bahamas               0x0B
#define K_Bahrain               0x0C
#define K_Bangladesh            0x0D
#define K_Barbados              0x0E
#define K_Belarus               0x1E
#define K_Belgium               0x0F
#define K_Belize                0x10
#define K_Benin                 0x11
#define K_Bermuda               0x12
#define K_Bhutan                0x13
#define K_BosniaHerzegovina     0x14
#define K_Botswana              0x15
#define K_Brazil                0x16
#define K_BritishVirginIsland   0x19
#define K_BruneiDarussalam      0x1A
#define K_Bulgaria              0x1B
#define K_BurkinaFaso           0xB6
#define K_Burundi               0x1D
#define K_Cameroon              0x1F
#define K_Canda                 0x20
#define K_CaymanIslands         0x22
#define K_CentralAfricanRep     0x23
#define K_ChannelIslands        0x5D
#define K_Chile                 0x25
#define K_China                 0x26
#define K_Columbia              0x27
#define K_Comoros               0x28
#define K_CostaRica             0x2B
#define K_CoteDIvoire           0x5A
#define K_Croatia               0xFA
#define K_Cuba                  0x2C
#define K_Cyprus                0x2D
#define K_CzechRepublic         0x2E
#define K_Denmark               0x31
#define K_DiegoGarcia           0xE2
#define K_Djibouti              0x32
#define K_Dominica              0x34
#define K_DmoinicanRep          0x33
#define K_Ecuador               0x35
#define K_Egypt                 0x36
#define K_ElSalvador            0x37
#define K_Estonia               0xF9
#define K_Ethiopia              0x38
#define K_FalklandIslands       0x3A
#define K_FaroeIslands          0xE3
#define K_Fiji                  0x3B
#define K_Finland               0x3C
#define K_France                0x3D
#define K_FrenchPolynesia       0x3E
#define K_Gabon                 0x40
#define K_Gambia                0x41
#define K_Germany               0x42
#define K_Ghana                 0x44
#define K_Gibraltar             0x45
#define K_Greece                0x46
#define K_Greenland             0xE4
#define K_Grenada               0x47
#define K_Guadeloupe            0xE5
#define K_Guatemala             0x49
#define K_Guernsey              0x4A
#define K_Guiana                0xE6
#define K_Guinea                0x4B
#define K_Guayana               0x4D
#define K_Honduras              0x4F
#define K_HongKong              0x50
#define K_Hungary               0x51
#define K_Iceland               0x52
#define K_India                 0x53
#define K_Indonesia             0x54
#define K_Iran                  0x55
#define K_Iraq                  0x56
#define K_Ireland               0x57
#define K_Israel                0x58
#define K_Italy                 0x59
#define K_Jamaica               0x5B
#define K_Japan                 0x00
#define K_Jersey                0x5D
#define K_Jordan                0x5E
#define K_Kenya                 0x5F
#define K_Kiribati              0x60
#define K_Korea                 0x61
#define K_Kuwait                0x62
#define K_Kyrgyxstan            0xE7
#define K_Lao                   0x63
#define K_Latvia                0xF8
#define K_Lebanon               0x64
#define K_Liberia               0x66
#define K_Libya                 0x67
#define K_Litechtenstein        0x68
#define K_Lithuania             0xF7
#define K_Luxembourg            0x69
#define K_Macau                 0x6A
#define K_Macedonia             0xE8
#define K_Madagascar            0x6B
#define K_Malawi                0x6D
#define K_Malaysia              0x6C
#define K_Maldives              0x6E
#define K_Mali                  0x6F
#define K_Malta                 0x70
#define K_ManIsleOf             0xE9
#define K_Martinique            0xEA
#define K_Mauritania            0x71
#define K_Mauritius             0x72
#define K_Mayotte               0xEB
#define K_Mexico                0x73
#define K_Moldova               0xEC
#define K_Monaco                0x74
#define K_Montserrat            0x76
#define K_Morocco               0x77
#define K_Mozambique            0x78
#define K_Namibia               0xED
#define K_Nauru                 0x79
#define K_Nepal                 0x7A
#define K_Netherlands           0x7B
#define K_NewCaledonia          0x7D
#define K_NewZealand            0x7E
#define K_Nicaragua             0x7F
#define K_Niger                 0x80
#define K_Nigeria               0x81
#define K_Norway                0x82
#define K_Oman                  0x83
#define K_Pakistan              0x84
#define K_Palestine             0xEE
#define K_Panama                0x85
#define K_PupuaNewGuinea        0x86
#define K_Paraguay              0x87
#define K_Peru                  0x88
#define K_Philippines           0x89
#define K_Poland                0x8A
#define K_Portugal              0x8B
#define K_PuertoRico            0x8C
#define K_Qatar                 0x8D
#define K_Reunion               0xEF
#define K_Romina                0x8E
#define K_Russia                0xB8
#define K_Rwanda                0x8F
#define K_SHelena               0x92
#define K_SKittsNevis           0x90
#define K_SLucia                0x93
#define K_SPierreMiquelon       0xF0
#define K_SVincentGrenadines    0x97
#define K_SanMarino             0x94
#define K_SaoTomePrincipe       0x96
#define K_SaudiArabia           0x98
#define K_Senegal               0x99
#define K_SerbiaMontenegro      0xF1
#define K_Seychelles            0x9A
#define K_SierraLeone           0x9B
#define K_Singapore             0x9C
#define K_Slovakia              0xC5
#define K_Slovenia              0xC6
#define K_Solomon               0x9D
#define K_SouthAfrica           0x9F
#define K_Spain                 0xA0
#define K_SriLanka              0xA1
#define K_Suriname              0xA3
#define K_Swaziland             0xA4
#define K_Sweden                0xA5
#define K_Switzerland           0xA6
#define K_Syria                 0xA7
#define K_Taiwan                0xFE
#define K_Tajikistan            0xF2
#define K_Tanzania              0xA8
#define K_Thailand              0xA9
#define K_TrinidadTobago        0xAC
#define K_Tunisia               0xAD
#define K_Turkey                0xAE
#define K_Turkmenistan          0xF3
#define K_TurksCaicosIslands    0xAF
#define K_Uganda                0xB1
#define K_Ukraine               0xB2
#define K_Uae                   0xB3
#define K_UnitedKingdom         0xB4
#define K_Usa                   0xB5
#define K_Uruguay               0xB7
#define K_Vanuatu               0xB9
#define K_Vietnam               0xBC
#define K_WallisFutuna          0xBD
#define K_WesternSamoa          0xBE
#define K_Yemen                 0xC0
#define K_Yugoslavia            0xC1
#define K_Zambia                0xC3
#define K_Zimbabwe              0xC4

                /****** system func routine ******/

#define   OS_FN_GET_VERSION         0
// BYTE * os_get_version(BYTE * a_ptr);
#define os_get_version(x)         sys_func1(OS_FN_GET_VERSION,(DWORD)(x))

                /****** keyboard routine ******/

#define K_Key0                  '0'
#define K_Key1                  '1'
#define K_Key2                  '2'
#define K_Key3                  '3'
#define K_Key4                  '4'
#define K_Key5                  '5'
#define K_Key6                  '6'
#define K_Key7                  '7'
#define K_Key8                  '8'
#define K_Key9                  '9'
#define K_KeyEnter              0x3a
#define K_KeyBackSp             0x3b
#define K_KeyClr                K_KeyBackSp
#define K_KeyCancel             0x3c
#define K_KeyFunc1              0x3d
#define K_KeyAlpha              K_KeyFunc1
#define K_KeyFunc2              0x3e
#define K_KeyFunc3              0x3f
#define K_KeyFunc4              0x40
#define K_KeyFunc5              0x41
#define K_KeySelect1            0x42
#define K_KeySelect2            0x43
#define K_KeySelect3            0x44
#define K_KeySelect4            0x45
#define K_KeyPower              0x46
#define K_KeyNull               0

#define   OS_FN_KBD_OPEN          (OS_FN_GET_VERSION + 1)
// BOOLEAN os_kbd_open(void * a_init_str);
#define os_kbd_open(x)            sys_func1(OS_FN_KBD_OPEN,(DWORD)(x))

#define   OS_FN_KBD_CLOSE         (OS_FN_KBD_OPEN + 1)
// void os_kbd_close();
#define os_kbd_close()            sys_func0(OS_FN_KBD_CLOSE)

#define   OS_FN_KBD_INKEY         (OS_FN_KBD_CLOSE + 1)
// BYTE os_kbd_inkey();
#define os_kbd_inkey()            sys_func0(OS_FN_KBD_INKEY)

#define   OS_FN_KBD_SCODE         (OS_FN_KBD_INKEY + 1)
// BYTE os_kbd_scode();
#define os_kbd_scode()            sys_func0(OS_FN_KBD_SCODE)

#define   OS_FN_KBD_GETKEY        (OS_FN_KBD_SCODE + 1)
// BYTE os_kbd_getkey();
#define os_kbd_getkey()           sys_func0(OS_FN_KBD_GETKEY)

                /****** display routine 2 ******/

#define   OS_FN_DISP_TEXTC        (OS_FN_KBD_GETKEY + 1)
// void os_disp_textc(DWORD a_color);
#define os_disp_textc(x)           sys_func1(OS_FN_DISP_TEXTC,(DWORD)(x))

#define   OS_FN_DISP_BACKC        (OS_FN_DISP_TEXTC + 1)
// void os_disp_backc(DWORD a_color);
#define os_disp_backc(x)           sys_func1(OS_FN_DISP_BACKC,(DWORD)(x))

                /****** timer routine ******/
#define K_NoTimerAvailable        0xff
#define   OS_FN_TIMER_OPEN        (OS_FN_DISP_BACKC + 1)
// DWORD os_timer_open();
#define os_timer_open()           sys_func0(OS_FN_TIMER_OPEN)

#define   OS_FN_TIMER_CLOSE       (OS_FN_TIMER_OPEN + 1)
// void os_timer_close(DWORD a_idx);
#define os_timer_close(x)         sys_func1(OS_FN_TIMER_CLOSE,(DWORD)(x))

#define   OS_FN_TIMER_SET         (OS_FN_TIMER_CLOSE + 1)
// void os_timer_set(DWORD a_idx,DWORD a_value);
#define os_timer_set(x,y)         sys_func2(OS_FN_TIMER_SET,(DWORD)(x),(DWORD)(y))

#define   OS_FN_TIMER_GET         (OS_FN_TIMER_SET + 1)
// DWORD os_timer_get(DWORD a_idx);
#define os_timer_get(x)           sys_func1(OS_FN_TIMER_GET,(DWORD)(x))

                /****** beeper routine ******/

#define   OS_FN_BUZ_OPEN          (OS_FN_TIMER_GET + 1)
// BOOLEAN os_beep_open(DWORD a_select);
#define os_beep_open(x)           sys_func1(OS_FN_BUZ_OPEN,(DWORD)(x))

#define   OS_FN_BUZ_CLOSE         (OS_FN_BUZ_OPEN + 1)
// void os_beep_close();
#define os_beep_close()           sys_func0(OS_FN_BUZ_CLOSE)

#define   OS_FN_BUZ_BEEP          (OS_FN_BUZ_CLOSE + 1)
// void os_beep_sound(WORD a_ontime,WORD a_offtime,WORD a_count);
#define os_beep_sound(x,y,z)      sys_func3(OS_FN_BUZ_BEEP,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_BUZ_STATE         (OS_FN_BUZ_BEEP + 1)
// void os_beep_state(DWORD a_state);
#define os_beep_state(x)          sys_func1(OS_FN_BUZ_STATE,(DWORD)(x))

                /****** rtc routine ******/

#define   OS_FN_RTC_GET           (OS_FN_BUZ_STATE + 1)
// BOOLEAN os_rtc_get(BYTE * a_dtg);
#define os_rtc_get(x)             sys_func1(OS_FN_RTC_GET,(DWORD)(x))

#define   OS_FN_RTC_SET           (OS_FN_RTC_GET + 1)
// BOOLEAN os_rtc_set(BYTE * a_dtg);
#define os_rtc_set(x)             sys_func1(OS_FN_RTC_SET,(DWORD)(x))

                /****** display routine 1 ******/

#define K_DispTimeoutDef        10          // default LCD bl off time
#define K_DispBlOnAlways        0xff        // always on
#define K_DispBlOffAlways       0           // always off
#define K_DispBlOnNow           0x100       // on now
#define K_DispBlOffNow          0x101       // off now
#define K_DispBrightnessDef     5
#define K_DispBrightnessMax     7
#define K_DispWidthInPixel      320
#define K_DispHeightInPixel     240
#define K_DispMemorySize        (K_DispWidthInPixel*K_DispHeightInPixel*2)
#define K_LogoSize              (K_DispWidthInPixel*(K_DispHeightInPixel-24)*2+8)

#define K_CurRight              1
#define K_CurLeft               2
#define K_CurDown               3
#define K_CurUp                 4
#define K_DelEol                5
#define K_Bell                  7
#define K_BackSp                8
#define K_NewLine               10
#define K_CurHome               11
#define K_ClrHome               12
#define K_CReturn               13
#define K_PushCursor            14
#define K_PopCursor             15
#define K_Esc                   27
#define K_RevOn                 0x1C
#define K_RevOff                0x1D
#define K_SelSmFont             0x10
#define K_SelBigFont            0x11
#define K_SelSpFont             0x12
#define K_SelUser8x16           0x13
#define K_SelUser8x8            0x14
#define K_SelUser6x8            0x15
#define K_SelUser8x16T          0x16
#define K_SetTextColor          0x17
#define K_SetTextBackColor      0x18
#define K_RefreshIcon           0x19      // mfg only

// Line No
#define K_Line1                 0x01
#define K_Line2                 0x02
#define K_Line3                 0x03
#define K_Line4                 0x04
#define K_Line5                 0x05
#define K_Line6                 0x06
#define K_Line7                 0x07
#define K_Line8                 0x08
#define K_Line9                 0x09
#define K_LineMax               K_Line9

// Column no
#define K_Column1               0x01
#define K_Column2               0x02
#define K_Column3               0x03
#define K_Column4               0x04
#define K_Column5               0x05
#define K_Column6               0x06
#define K_Column7               0x07
#define K_Column8               0x08
#define K_Column9               0x09
#define K_Column10              0x0A
#define K_Column11              0x0B
#define K_Column12              0x0C
#define K_Column13              0x0D
#define K_Column14              0x0E
#define K_Column15              0x0F
#define K_Column16              0x10
#define K_Column17              0x11
#define K_Column18              0x12
#define K_Column19              0x13
#define K_Column20              0x14
#define K_Column21              0x15
#define K_Column22              0x16
#define K_Column23              0x17
#define K_Column24              0x18
#define K_Column25              0x19
#define K_Column26              0x1A

// Line No Pos with column1 offset
#define K_PosLine1              ((0x01<<8)+K_Column1)
#define K_PosLine2              ((0x02<<8)+K_Column1)
#define K_PosLine3              ((0x03<<8)+K_Column1)
#define K_PosLine4              ((0x04<<8)+K_Column1)
#define K_PosLine5              ((0x05<<8)+K_Column1)
#define K_PosLine6              ((0x06<<8)+K_Column1)
#define K_PosLine7              ((0x07<<8)+K_Column1)
#define K_PosLine8              ((0x08<<8)+K_Column1)
#define K_PosLine9              ((0x09<<8)+K_Column1)

#define K_MaxSmPerLine          20
#define K_MaxSpPerLine          26

// set pos byte seq = K_ESC + Line no + Column no
// set text color seq = K_SetTextColor + high-byte color + low-byte color
// set text background colot seq = K_SetTextBackColor + high-byte color + low-byte color
// cursor = high-byte is row and low byte is column

typedef struct {
  WORD  w_hstart; // start pixel horizontal start position
  WORD  w_vstart; // start pixel vertical start position
  WORD  w_hwidth; // horizontal pixel width, x
  WORD  w_vwidth; // vertical pixel width, y
  WORD  p_data[1];   // pointer to graphical data to be saved or displayed, len = x * y WORD
} T_LCDG;

/*
  horizontal 320 pixels
  vertical   216 pixels
  2-byte pixels
    bit 15 -  intensity bit which will be appended at the lsb of each 5-bit color to make up 6-bit per
              primary color.
    bit 0-4   Red
    bit 5-9   Green
    bit 10-14 Blue
*/

#define   OS_FN_DISP_OPEN         (OS_FN_RTC_SET + 1)
// BOOLEAN os_disp_open();
#define os_disp_open()            sys_func0(OS_FN_DISP_OPEN)

#define   OS_FN_DISP_CLOSE        (OS_FN_DISP_OPEN + 1)
// void os_disp_close();
#define os_disp_close()           sys_func0(OS_FN_DISP_CLOSE)

#define   OS_FN_DISP_PUTC         (OS_FN_DISP_CLOSE + 1)
// BYTE os_disp_putc(DWORD a_c);
#define os_disp_putc(x)           sys_func1(OS_FN_DISP_PUTC,(DWORD)(x))

#define   OS_FN_DISP_PUTS         (OS_FN_DISP_PUTC + 1)
// void os_disp_puts(void * a_str);
#define os_disp_puts(x)           sys_func1(OS_FN_DISP_PUTS,(DWORD)(x))

#define   OS_FN_DISP_PUTN         (OS_FN_DISP_PUTS + 1)
// void os_disp_putn(void * a_str,DWORD a_count);
#define os_disp_putn(x,y)         sys_func2(OS_FN_DISP_PUTN,(DWORD)(x),(DWORD)(y))

#define   OS_FN_DISP_GETG         (OS_FN_DISP_PUTN + 1)
// BOOLEAN os_disp_getg(T_LCDG * a_lcdg);
#define os_disp_getg(x)           sys_func1(OS_FN_DISP_GETG,(DWORD)(x))

#define   OS_FN_DISP_PUTG        (OS_FN_DISP_GETG + 1)
// BOOLEAN os_disp_putg(T_LCDG * a_lcdg);
#define os_disp_putg(x)           sys_func1(OS_FN_DISP_PUTG,(DWORD)(x))

#define   OS_FN_DISP_BRIGHTNESS   (OS_FN_DISP_PUTG + 1)
// DWORD os_disp_brightness(DWORD a_brightness); // 0-7, 7 = brightest
#define os_disp_brightness(x)     sys_func1(OS_FN_DISP_BRIGHTNESS,(DWORD)(x))

/*
channel icon image bit pattern
bit 0 - aux1 tx, red solid circle
bit 1 - aux1 rx, green solid circle
bit 2 - auxdbg tx, red hollow triangle
bit 3 - auxdbg rx, green hollow triangle
bit 4 - tcp channel 1 tx, red solid star
bit 5 - tcp channel 1 rx, green solid star
bit 6 - tcp channel 2 tx, red hollow diamond
bit 7 - tcp channel 2 rx, green hollow diamond
bit 8 - tcp channel 3 tx, red solid square
bit 9 - tcp channel 3 rx, green solid square
bit 10- tcp channel 4 tx, red hollow down triangle
bit 11- tcp channel 4 rx, green hollow down triangle
bit 12- udp channel 1 tx, red solid down triangle
bit 13- udp channel 1 rx, green solid down triangle
bit 14- udp channel 2 tx, red hollow square
bit 15- udp channel 2 rx, green hollow square
bit 16- usb slave tx, red solid diamond
bit 17- usb slave rx, green solid diamond
bit 18- usb host tx, red hollow star
bit 19- usb host rx, green hollow star
bit 20- aux3 tx, red solid triangle
bit 21- aux3 rx, green solid triangle
bit 22- mcom modem tx, red hollow circle
bit 23- mcom modem rx, green hollow circle

signal icon image bit pattern
bit 0 - signal 0
bit 1 - signal 1
bit 2 - signal 2
bit 3 - signal 3
bit 4 - signal 4
bit 5 - antenna
bit 8 - battery frame
bit 9 - battery level 1
bit 10- battery level 2
bit 11- battery level 3
bit 12- battery level 4
*/
#define K_DispIconSignal0     0x0001
#define K_DispIconSignal1     0x0002
#define K_DispIconSignal2     0x0004
#define K_DispIconSignal3     0x0008
#define K_DispIconSignal4     0x0010
#define K_DispIconAntenna     0x0020
#define K_DispIconBFrame      0x0100
#define K_DispIconBLevel1     0x0200
#define K_DispIconBLevel2     0x0400
#define K_DispIconBLevel3     0x0800
#define K_DispIconBLevel4     0x1000
#define K_DispIconBCharging   0x2000

#define   OS_FN_DISP_ICON_SET    (OS_FN_DISP_BRIGHTNESS + 1)
// DWORD os_disp_icon_set(DWORD a_channel,DWORD a_signal);  // system only
#define os_disp_icon_set(x,y)    sys_func2(OS_FN_DISP_ICON_SET,(DWORD)(x),(DWORD)(y))

#define   OS_FN_DISP_ICON_CLEAR  (OS_FN_DISP_ICON_SET + 1)
// DWORD os_disp_icon_clear(DWORD a_channel,DWORD a_signal);  // system only
#define os_disp_icon_clear(x,y)  sys_func2(OS_FN_DISP_ICON_CLEAR,(DWORD)(x),(DWORD)(y))

          /****** aux 1 routine ******/

#define   OS_FN_AUX1_OPEN         (OS_FN_DISP_ICON_CLEAR + 1)
/*
  Offset 0: length of data list (BYTE)
  Offset 1: Communication mode
            RS232 :
              0x00 = AUTO protocol for STX or ENAX STX
              0x01 = STX protocol - STX SEQ DATA ETX LRC
              0x02 = ENAC STX - STX LEN1 LEN2 DATA ETX LRC
              0x03 = SOH-LRC
              0x04 = SOH-CRC
              0x05 = ASYNC protocol with no parity
              0x06 = ASYNC protocol with even parity
              0x07 = ASYNC protocol with odd parity
  Offset 2: Communication speed
              8 = 230400 bps
              7 = 115200 bps
              6 = 57600 bps
              5 = 38400 bps
              4 = 28800 bps
              3 = 19200 bps
              2 = 4800 bps
              1 = 2400 bps
              0 = 9600 bps (default)
            bit 4 -
              0 = 1 byte length field
              1 = 2 byte length field
  Offset 3: qualify time in 10ms for ASYNC,rx gap
  Offset 4: response gap
  Offset 5: tx gap
  Offset 6: retry min 1
*/
// BOOLEAN os_aux1_open(void * a_init_str);
#define os_aux1_open(x)           sys_func1(OS_FN_AUX1_OPEN,(DWORD)(x))

#define   OS_FN_AUX1_CLOSE        (OS_FN_AUX1_OPEN + 1)
// DWORD os_aux1_close(void);
#define os_aux1_close()           sys_func0(OS_FN_AUX1_CLOSE)

#define   OS_FN_AUX1_RXRDY        (OS_FN_AUX1_CLOSE + 1)
// BOOLEAN os_aux1_rxrdy(void);
#define os_aux1_rxrdy()           sys_func0(OS_FN_AUX1_RXRDY)

#define   OS_FN_AUX1_TXRDY        (OS_FN_AUX1_RXRDY + 1)
// BOOLEAN os_aux1_txrdy(void);
#define os_aux1_txrdy()           sys_func0(OS_FN_AUX1_TXRDY)

#define   OS_FN_AUX1_READ         (OS_FN_AUX1_TXRDY + 1)
// DWORD os_aux1_read(BYTE * a_buf); // app only
#define os_aux1_read(x)           sys_func1(OS_FN_AUX1_READ,(DWORD)(x))

#define   OS_FN_AUX1_SEND         (OS_FN_AUX1_READ + 1)
//BOOLEAN os_aux1_send(BYTE * a_buf,DWORD a_len); // app only
#define os_aux1_send(x,y)         sys_func2(OS_FN_AUX1_SEND,(DWORD)(x),(DWORD)(y))

#define   OS_FN_AUX1_STATUS       (OS_FN_AUX1_SEND + 1)
/*
  - error will be reset after reported
  bit 15        K_LinkEXIST
  bit 14        K_LinkONK_Line
  bit 13        K_LinkTXRDY
  bit 12        K_LinkRXRDY
  bit 11        K_LinkTX_ERROR
  bit 10        K_LinkRX_ERROR
  bit 8         K_LinkRX_OVERFLOW
*/
// DWORD os_aux1_status(void);
#define os_aux1_status()          sys_func0(OS_FN_AUX1_STATUS)

#define   OS_FN_AUX1_SREAD        (OS_FN_AUX1_STATUS + 1)
// T_BUF * os_aux1_sread(void);  // system only
#define os_aux1_sread()           ((T_BUF *)sys_func0(OS_FN_AUX1_SREAD))

#define   OS_FN_AUX1_SSEND        (OS_FN_AUX1_SREAD + 1)
// BOOLEAN os_aux1_ssend(T_BUF * a_ptr); // system only
#define os_aux1_ssend(x)          sys_func1(OS_FN_AUX1_SSEND,(DWORD)(x))

          /****** aux 2 routine, not support ******/

#define   OS_FN_AUX2_OPEN         (OS_FN_AUX1_SSEND + 1)
/*
  Offset 0: length of data list (BYTE)
  Offset 1: Communication mode
            RS232 :
              0x00 = AUTO protocol for STX or ENAX STX
              0x01 = STX protocol - STX SEQ DATA ETX LRC
              0x02 = ENAC STX - STX LEN1 LEN2 DATA ETX LRC
              0x03 = SOH-LRC
              0x04 = SOH-CRC
              0x05 = ASYNC protocol woth no parity
              0x06 = ASYNC protocol with even parity
              0x07 = ASYNC protocol with odd parity
  Offset 2: Communication speed
              8 = 230400 bps
              7 = 115200 bps
              6 = 57600 bps
              5 = 38400 bps
              4 = 28800 bps
              3 = 19200 bps
              2 = 4800 bps
              1 = 2400 bps
              0 = 9600 bps (default)
            bit 4 -
              0 = 1 byte length field
              1 = 2 byte length field
  Offset 3: qualify time in 10ms for ASYNC,rx gap
  Offset 4: response gap
  Offset 5: tx gap
  Offset 6: retry min 1
*/
// BOOLEAN os_aux2_open(void * a_init_str); not support
#define os_aux2_open(x)           sys_func1(OS_FN_AUX2_OPEN,(DWORD)(x))

#define   OS_FN_AUX2_CLOSE        (OS_FN_AUX2_OPEN + 1)
// DWORD os_aux2_close(); not support
#define os_aux2_close()           sys_func0(OS_FN_AUX2_CLOSE)

#define   OS_FN_AUX2_RXRDY        (OS_FN_AUX2_CLOSE + 1)
// BOOLEAN os_aux2_rxrdy(); not support
#define os_aux2_rxrdy()           sys_func0(OS_FN_AUX2_RXRDY)

#define   OS_FN_AUX2_TXRDY        (OS_FN_AUX2_RXRDY + 1)
// BOOLEAN os_aux2_txrdy(); not support
#define os_aux2_txrdy()           sys_func0(OS_FN_AUX2_TXRDY)

#define   OS_FN_AUX2_READ         (OS_FN_AUX2_TXRDY + 1)
// DWORD os_aux2_read(BYTE * a_buf); // app only not support
#define os_aux2_read(x)           sys_func1(OS_FN_AUX2_READ,(DWORD)(x))

#define   OS_FN_AUX2_SEND         (OS_FN_AUX2_READ + 1)
//BOOLEAN os_aux2_send(BYTE * a_buf,DWORD a_len); // app only not support
#define os_aux2_send(x,y)         sys_func2(OS_FN_AUX2_SEND,(DWORD)(x),(DWORD)(y))

#define   OS_FN_AUX2_STATUS       (OS_FN_AUX2_SEND + 1)
/*
  - error will be reset after reported
  bit 15        K_LinkEXIST
  bit 14        K_LinkOnline
  bit 13        K_LinkTXRDY
  bit 12        K_LinkRXRDY
  bit 11        K_LinkTX_ERROR
  bit 10        K_LinkRX_ERROR
  bit 8         K_LinkRX_OVERFLOW
*/
// DWORD os_aux1_status(); not support
#define os_aux2_status()          sys_func0(OS_FN_AUX2_STATUS)

#define   OS_FN_AUX2_SREAD        (OS_FN_AUX2_STATUS + 1)
// T_BUF * os_aux2_sread();  // system only not support
#define os_aux2_sread()           ((T_BUF *)sys_func0(OS_FN_AUX2_SREAD))

#define   OS_FN_AUX2_SSEND        (OS_FN_AUX2_SREAD + 1)
// BOOLEAN os_aux2_ssend(T_BUF * a_ptr); // system only not support
#define os_aux2_ssend(x)          sys_func1(OS_FN_AUX2_SSEND,(DWORD)(x))

          /****** auxdbg routine 1 ******/

#define   OS_FN_AUXDBG_OPEN       (OS_FN_AUX2_SSEND + 1)
/*
  Offset 0: length of data list (BYTE)
  Offset 1: Communication mode
            RS232 :
              0x00 = AUTO protocol for STX or ENAX STX
              0x01 = STX protocol - STX SEQ DATA ETX LRC
              0x02 = ENAC STX - STX LEN1 LEN2 DATA ETX LRC
              0x03 = SOH-LRC
              0x04 = SOH-CRC
              0x05 = ASYNC protocol woth no parity
              0x06 = ASYNC protocol with even parity
              0x07 = ASYNC protocol with odd parity
  Offset 2: Communication speed
              8 = 230400 bps
              7 = 115200 bps
              6 = 57600 bps
              5 = 38400 bps
              4 = 28800 bps
              3 = 19200 bps
              2 = 4800 bps
              1 = 2400 bps
              0 = 9600 bps (default)
            bit 4 -
              0 = 1 byte length field
              1 = 2 byte length field
  Offset 3: qualify time in 10ms for ASYNC,rx gap
  Offset 4: response gap
  Offset 5: tx gap
  Offset 6: retry min 1
*/
// BOOLEAN os_auxdbg_open(void * a_init_str);
#define os_auxdbg_open(x)         sys_func1(OS_FN_AUXDBG_OPEN,(DWORD)(x))

#define   OS_FN_AUXDBG_CLOSE      (OS_FN_AUXDBG_OPEN + 1)
// DWORD os_auxdbg_close(void);
#define os_auxdbg_close()         sys_func0(OS_FN_AUXDBG_CLOSE)

#define   OS_FN_AUXDBG_RXRDY      (OS_FN_AUXDBG_CLOSE + 1)
// BOOLEAN os_auxdbg_rxrdy(void);
#define os_auxdbg_rxrdy()         sys_func0(OS_FN_AUXDBG_RXRDY)

#define   OS_FN_AUXDBG_TXRDY      (OS_FN_AUXDBG_RXRDY + 1)
// BOOLEAN os_auxdbg_txrdy(void);
#define os_auxdbg_txrdy()         sys_func0(OS_FN_AUXDBG_TXRDY)

#define   OS_FN_AUXDBG_READ       (OS_FN_AUXDBG_TXRDY + 1)
// DWORD os_auxdbg_read(BYTE *a_buf); // app only
#define os_auxdbg_read(x)        sys_func1(OS_FN_AUXDBG_READ,(DWORD)(x))

#define   OS_FN_AUXDBG_SEND       (OS_FN_AUXDBG_READ + 1)
// BOOLEAN os_auxdbg_send(BYTE *a_buf,DWORD a_len); // app only
#define os_auxdbg_send(x,y)       sys_func2(OS_FN_AUXDBG_SEND,(DWORD)(x),(DWORD)(y))

#define   OS_FN_AUXDBG_STATUS     (OS_FN_AUXDBG_SEND + 1)
/*
  - error will be reset after reported
  bit 15        K_LinkEXIST
  bit 14        K_LinkONK_Line
  bit 13        K_LinkTXRDY
  bit 12        K_LinkRXRDY
  bit 11        K_LinkTX_ERROR
  bit 10        K_LinkRX_ERROR
  bit 8         K_LinkRX_OVERFLOW
*/
// DWORD os_auxdbg_status();
#define os_auxdbg_status()        sys_func0(OS_FN_AUXDBG_STATUS)

                /****** encryption routine 1 ******/

#define   OS_FN_DESKEY            (OS_FN_AUXDBG_STATUS + 1)
// void os_deskey(void * a_key);
#define os_deskey(x)              sys_func1(OS_FN_DESKEY,(DWORD)(x))

#define   OS_FN_DES               (OS_FN_DESKEY + 1)
// void os_des(void * a_msg);
#define os_des(x)                 sys_func1(OS_FN_DES,(DWORD)(x))

#define   OS_FN_DES2              (OS_FN_DES + 1)
// void os_des2(void * a_msg);
#define os_des2(x)                sys_func1(OS_FN_DES2,(DWORD)(x))

#define   OS_FN_3DESKEY           (OS_FN_DES2 + 1)
// void os_3deskey(void * a_key);
#define os_3deskey(x)             sys_func1(OS_FN_3DESKEY,(DWORD)(x))

#define   OS_FN_3DES              (OS_FN_3DESKEY + 1)
// void os_3des(void * a_msg);
#define os_3des(x)                sys_func1(OS_FN_3DES,(DWORD)(x))

#define   OS_FN_3DES2             (OS_FN_3DES + 1)
// void os_3des2(void * a_msg);
#define os_3des2(x)               sys_func1(OS_FN_3DES2,(DWORD)(x))

#define   OS_FN_3KDESKEY          (OS_FN_3DES2 + 1)
// void os_3kdeskey(void * a_key);
#define os_3kdeskey(x)            sys_func1(OS_FN_3KDESKEY,(DWORD)(x))

#define   OS_FN_3KDES             (OS_FN_3KDESKEY + 1)
// void os_3kdes(void * a_msg);
#define os_3kdes(x)               sys_func1(OS_FN_3KDES,(DWORD)(x))

#define   OS_FN_3KDES2            (OS_FN_3KDES + 1)
// void os_3kdes2(void * a_msg);
#define os_3kdes2(x)              sys_func1(OS_FN_3KDES2,(DWORD)(x))

#define   OS_FN_RSA               (OS_FN_3KDES2 + 1)
// BOOLEAN os_rsa(void * a_msg,T_KEY * a_key);
#define os_rsa(x,y)               sys_func2(OS_FN_RSA,(DWORD)(x),(DWORD)(y))

#define   OS_FN_SHA1              (OS_FN_RSA + 1)
// void os_sha1(void * a_omsg,void * a_imsg,DWORD a_len);
#define os_sha1(x,y,z)            sys_func3(OS_FN_SHA1,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_MD5_INIT          (OS_FN_SHA1 + 1)
// void os_md5_init(T_MD5_CONTEXT * a_c);
#define os_md5_init(x)            sys_func1(OS_FN_MD5_INIT,(DWORD)x)

#define   OS_FN_MD5_PROCESS       (OS_FN_MD5_INIT + 1)
// void os_md5_process(T_MD5_CONTEXT * a_c, BYTE* a_input, DWORD a_length);
#define os_md5_process(x,y,z)     sys_func3(OS_FN_MD5_PROCESS,(DWORD)x,(DWORD)y,z)

#define   OS_FN_MD5_FINISH        (OS_FN_MD5_PROCESS + 1)
// void os_md5_finish(T_MD5_CONTEXT * a_c, BYTE* a_digest);
#define os_md5_finish(x,y)        sys_func2(OS_FN_MD5_FINISH,(DWORD)x,(DWORD)y)

#define   OS_FN_SHA1_INIT         (OS_FN_MD5_FINISH + 1)
// void os_sha1_init(T_SHA1_CONTEXT * a_c);
#define os_sha1_init(x)           sys_func1(OS_FN_SHA1_INIT,(DWORD)x)

#define   OS_FN_SHA1_PROCESS      (OS_FN_SHA1_INIT + 1)
// void os_sha1_process(T_SHA1_CONTEXT * a_c, BYTE* a_input, DWORD a_length);
#define os_sha1_process(x,y,z)    sys_func3(OS_FN_SHA1_PROCESS,(DWORD)x,(DWORD)y,z)

#define   OS_FN_SHA1_FINISH       (OS_FN_SHA1_PROCESS + 1)
// void os_sha1_finish(T_SHA1_CONTEXT * a_c, BYTE * a_digest);
#define os_sha1_finish(x,y)       sys_func2(OS_FN_SHA1_FINISH,(DWORD)x,(DWORD)y)

#define   OS_FN_RC4_SETUP         (OS_FN_SHA1_FINISH + 1)
// void os_rc4_setup(T_RC4_STATE * a_c, T_KEY * a_input, DWORD a_length);
#define os_rc4_setup(x,y,z)       sys_func3(OS_FN_RC4_SETUP,(DWORD)x,(DWORD)y,z)

#define   OS_FN_RC4_ENCRYPT       (OS_FN_RC4_SETUP + 1)
// void os_rc4_encrypt(T_RC4_STATE_T * a_c, BYTE * a_input, DWORD a_length);
#define os_rc4_encrypt(x,y,z)      sys_func3(OS_FN_RC4_ENCRYPT,(DWORD)x,(DWORD)y,(DWORD)(z))

#define   os_md4_init             os_md5_init

#define   OS_FN_MD4_PROCESS       (OS_FN_RC4_ENCRYPT + 1)
// void os_md4_process(T_MD4_CONTEXT * a_c, BYTE * a_input, DWORD a_length);
#define os_md4_process(x,y,z)     sys_func3(OS_FN_MD4_PROCESS,(DWORD)x,(DWORD)y,z)

#define   OS_FN_MD4_FINISH        (OS_FN_MD4_PROCESS + 1)
// void os_md4_finish(T_MD4_CONTEXT * a_c, BYTE * a_digest);
#define os_md4_finish(x,y)        sys_func2(OS_FN_MD4_FINISH,(DWORD)x,(DWORD)y)

                /****** HSM routine 1 ******/

#define   OS_FN_KEY_SAVE          (OS_FN_MD4_FINISH + 1)
// void os_key_save(void * a_key,DWORD a_addr,DWORD a_mode);
#define os_key_save(x,y,z)        sys_func3(OS_FN_KEY_SAVE,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_KEY_GENKEY        (OS_FN_KEY_SAVE + 1)
// void os_key_genkey(void * a_ekey);
#define os_key_genkey(x)          sys_func1(OS_FN_KEY_GENKEY,(DWORD)(x))

#define   OS_FN_KEY_3GENKEY       (OS_FN_KEY_GENKEY + 1)
// void os_key_3genkey(void * a_ekey);
#define os_key_3genkey(x)         sys_func1(OS_FN_KEY_3GENKEY,(DWORD)(x))

#define   OS_FN_KEY_3KGENKEY      (OS_FN_KEY_3GENKEY + 1)
// void os_key_3kgenkey(void * a_ekey);
#define os_key_3kgenkey(x)        sys_func1(OS_FN_KEY_3KGENKEY,(DWORD)(x))

#define   OS_FN_KEY_FLUSH         (OS_FN_KEY_3KGENKEY + 1)
// BOOLEAN os_key_flush();        // system use only
#define os_key_flush()            sys_func0(OS_FN_KEY_FLUSH)

#define   OS_FN_KEY_DUKPT_INJECT  (OS_FN_KEY_FLUSH + 1)
// void os_key_dukpt_key_inject(T_KEYBANK *a_pkb,BYTE *a_key);
#define os_key_dukpt_key_inject(x,y) sys_func2(OS_FN_KEY_DUKPT_INJECT,(DWORD)(x),(DWORD)(y))

#define   OS_FN_KEY_DUKPT_ENCRYPT (OS_FN_KEY_DUKPT_INJECT + 1)
// BOOLEAN os_key_dukpt_encrypt(T_KEYBANK *a_pkb,BYTE *a_pinblock,BYTE *a_ksn);
#define os_key_dukpt_encrypt(x,y,z) sys_func3(OS_FN_KEY_DUKPT_ENCRYPT,(DWORD)(x),(DWORD)(y),(DWORD)(z))

                /****** thread routine ******/

#define   OS_FN_THREAD_OPEN       (OS_FN_KEY_DUKPT_ENCRYPT + 1)
// BOOLEAN os_thread_open(); // system use only
#define os_thread_open()          sys_func0(OS_FN_THREAD_OPEN)

#define   OS_FN_THREAD_CLOSE      (OS_FN_THREAD_OPEN + 1)
// BOOLEAN os_thread_close(); // system use only
#define os_thread_close()         sys_func0(OS_FN_THREAD_CLOSE)

#define   OS_FN_THREAD_CREATE     (OS_FN_THREAD_CLOSE + 1)
// BOOLEAN os_thread_create(DWORD a_address,DWORD a_resolution);
#define os_thread_create(x,y)     sys_func2(OS_FN_THREAD_CREATE,(DWORD)(x),(DWORD)(y))

#define   OS_FN_THREAD_DELETE     (OS_FN_THREAD_CREATE + 1)
// BOOLEAN os_thread_delete(DWORD a_address);
#define os_thread_delete(x)       sys_func1(OS_FN_THREAD_DELETE,(DWORD)(x))

#define   OS_FN_THREAD_LOCK       (OS_FN_THREAD_DELETE + 1)
//void os_thread_lock();
#define os_thread_lock()          sys_func0(OS_FN_THREAD_LOCK)

#define   OS_FN_THREAD_UNLOCK     (OS_FN_THREAD_LOCK + 1)
//void os_thread_unlock()
#define os_thread_unlock()        sys_func0(OS_FN_THREAD_UNLOCK)

#define   OS_FN_THREAD_PUSH       (OS_FN_THREAD_UNLOCK + 1)
//void os_thread_push()
#define os_thread_push()          sys_func0(OS_FN_THREAD_PUSH)

#define   OS_FN_THREAD_POP        (OS_FN_THREAD_PUSH + 1)
//void os_thread_pop()
#define os_thread_pop()           sys_func0(OS_FN_THREAD_POP)

#define   OS_FN_THREAD_RETURN     (OS_FN_THREAD_POP + 1)
//void os_thread_return()
#define os_thread_return()        sys_func0(OS_FN_THREAD_RETURN)

                /****** buffer manipulation routine for system use only ******/

#define   OS_FN_BUFMAN_ALLOC    (OS_FN_THREAD_RETURN + 1)
//T_BUF * os_bufman_alloc(DWORD a_size);
#define os_bufman_alloc(x)      (T_BUF *)sys_func1(OS_FN_BUFMAN_ALLOC,(DWORD)(x))

#define   OS_FN_BUFMAN_FREE     (OS_FN_BUFMAN_ALLOC + 1)
//void os_bufman_free(T_BUF ** a_ptr);
#define os_bufman_free(x)       sys_func1(OS_FN_BUFMAN_FREE,(DWORD)(x))

#define   OS_FN_BUFMAN_REALLOC  (OS_FN_BUFMAN_FREE + 1)
//T_BUF * os_bufman_realloc(T_BUF ** a_ptr,DWORD a_size);
#define os_bufman_realloc(x,y)  (T_BUF *)sys_func2(OS_FN_BUFMAN_REALLOC,(DWORD)(x),(DWORD)(y))

#define   OS_FN_BUFMAN_SHRINK   (OS_FN_BUFMAN_REALLOC + 1)
//void os_bufman_shrink(T_BUF * a_ptr);
#define os_bufman_shrink(x)     sys_func1(OS_FN_BUFMAN_SHRINK,(DWORD)(x))

                /****** pseudo led routine ******/
/*
channel icon image bit pattern
bit 0 - aux1 tx, red solid circle
bit 1 - aux1 rx, green solid circle
bit 2 - auxdbg tx, red hollow triangle
bit 3 - auxdbg rx, green hollow triangle
bit 4 - tcp channel 1 tx, red solid star
bit 5 - tcp channel 1 rx, green solid star
bit 6 - tcp channel 2 tx, red hollow diamond
bit 7 - tcp channel 2 rx, green hollow diamond
bit 8 - tcp channel 3 tx, red solid square
bit 9 - tcp channel 3 rx, green solid square
bit 10- tcp channel 4 tx, red hollow down triangle
bit 11- tcp channel 4 rx, green hollow down triangle
bit 12- udp channel 1 tx, red solid down triangle
bit 13- udp channel 1 rx, green solid down triangle
bit 14- udp channel 2 tx, red hollow square
bit 15- udp channel 2 rx, green hollow square
bit 16- usb slave tx, red solid diamond
bit 17- usb slave rx, green solid diamond
bit 18- usb host tx, red hollow star
bit 19- usb host rx, green hollow star
bit 20- aux3 tx, red solid triangle
bit 21- aux3 rx, green solid triangle
bit 22- mcom modem tx, red hollow circle
bit 23- mcom modem rx, green hollow circle

signal icon image bit pattern
bit 0 - signal 0
bit 1 - signal 1
bit 2 - signal 2
bit 3 - signal 3
bit 4 - signal 4
bit 5 - antenna
bit 8 - battery frame
bit 9 - battery level 1
bit 10- battery level 2
bit 11- battery level 3
bit 12- battery level 4
*/
#define K_Aux1Tx                0
#define K_Aux1Rx                1
#define K_AuxDbgTx              2
#define K_AuxDbgRx              3
#define K_Tcp1Tx                4
#define K_Tcp1Rx                5
#define K_Tcp2Tx                6
#define K_Tcp2Rx                7
#define K_Tcp3Tx                8
#define K_Tcp3Rx                9
#define K_Tcp4Tx                10
#define K_Tcp4Rx                11
#define K_Udp1Tx                12
#define K_Udp1Rx                13
#define K_Udp2Tx                14
#define K_Udp2Rx                15
#define K_UsbSTx                16
#define K_UsbSRx                17
#define K_UsbHTx                18
#define K_UsbHRx                19
#define K_Aux3Tx                20
#define K_Aux3Rx                21
#define K_MdmTx                 22
#define K_MdmRx                 23

#define K_LedMax                (K_MdmRx-K_Aux1Tx+1)
#define K_AllLed                (DWORD)-1

#define K_LedDelayMin           4

#define   OS_FN_LED_OPEN        (OS_FN_BUFMAN_SHRINK + 1)
// void os_led_open(void); system only
#define os_led_open()           sys_func0(OS_FN_LED_OPEN)

#define   OS_FN_LED_CLOSE       (OS_FN_LED_OPEN + 1)
// void os_led_close(void); system only
#define os_led_close()          sys_func0(OS_FN_LED_CLOSE)

#define   OS_FN_LED_STATUS      (OS_FN_LED_CLOSE + 1)
// DWORD os_led_status(DWORD a_which);
#define os_led_status(x)        sys_func1(OS_FN_LED_STATUS,(DWORD)(x))

#define   OS_FN_LED_SET         (OS_FN_LED_STATUS + 1)
// void os_led_set(DWORD a_which);system only
#define os_led_set(x)           sys_func1(OS_FN_LED_SET,(DWORD)(x))

#define   OS_FN_LED_CLEAR       (OS_FN_LED_SET + 1)
// void os_led_clear(DWORD a_which);system only
#define os_led_clear(x)         sys_func1(OS_FN_LED_CLEAR,(DWORD)(x))

#define   OS_FN_LED_ON          (OS_FN_LED_CLEAR + 1)
// void os_led_on(DWORD a_which,DWORD a_delay);system only
#define os_led_on(x,y)          sys_func2(OS_FN_LED_ON,(DWORD)(x),(DWORD)(y))

                /****** app security routine ******/

#define   OS_FN_GET_CHALLENGE  (OS_FN_LED_ON + 1)
// void os_get_challenge(BYTE * a_dest);
#define os_get_challenge(x)     sys_func1(OS_FN_GET_CHALLENGE,(DWORD)(x))

#define   K_AuthAppSec          0
#define   K_AuthAppDll          1

#define   OS_FN_AUTH            (OS_FN_GET_CHALLENGE + 1)
// BOOLEAN os_security_auth(DWORD a_auth_type,BYTE * a_cryptogram);
#define os_auth(x,y)            sys_func2(OS_FN_AUTH,(DWORD)(x),(DWORD) (y))

                /****** app search routine ******/

#define   OS_FN_APP_SEARCH_ID   (OS_FN_AUTH + 1)
// BOOLEAN os_app_search_id(DWORD a_appid);
#define os_app_search_id(x)     sys_func1(OS_FN_APP_SEARCH_ID,(DWORD)(x))

#define   OS_FN_APP_SEARCH_NAME (OS_FN_APP_SEARCH_ID + 1)
// BOOLEAN os_app_search_name(BYTE * a_name,DWORD a_control);
#define os_app_search_name(x,y) sys_func2(OS_FN_APP_SEARCH_NAME,(DWORD)(x),(DWORD)(y))

#define   OS_FN_APP_GET_VIRGIN  (OS_FN_APP_SEARCH_NAME + 1)
// BOOLEAN os_app_get_virgin(DWORD a_appid);
#define os_app_get_virgin(x)     sys_func1(OS_FN_APP_GET_VIRGIN,(DWORD)(x))

#define   OS_FN_APP_CLR_VIRGIN  (OS_FN_APP_GET_VIRGIN + 1)
// void os_app_clear_virgin(DWORD a_appid);
#define os_app_clear_virgin(x)  sys_func1(OS_FN_APP_CLR_VIRGIN,(DWORD)(x))

                /****** memory allocate routine ******/

#define   OS_FN_MALLOC          (OS_FN_APP_CLR_VIRGIN + 1)
// void * os_malloc(DWORD a_size);
#define os_malloc(x)            (BYTE*)sys_func1(OS_FN_MALLOC,(DWORD)(x))

#define   OS_FN_REALLOC         (OS_FN_MALLOC + 1)
// void * os_realloc(void * a_bptr,DWORD a_size);
#define os_realloc(x,y)         (BYTE*)sys_func2(OS_FN_REALLOC,(DWORD)(x),(DWORD)(y))

#define   OS_FN_FREE            (OS_FN_REALLOC + 1)
// void os_free(void * a_bptr);
#define os_free(x)              sys_func1(OS_FN_FREE,(DWORD)(x))

                /****** file system routine 1 ******/

//==========================================================================
#define     K_ERR_FS_NOT_MOUNT              -2
#define     K_ERR_FS_STILL_MOUNT            -3
#define     K_ERR_FS_NOT_OPEN               -4
#define     K_ERR_FS_ACCESS_DENIED          -5
#define     K_ERR_FS_INVALID_VALUE          -6
#define     K_ERR_FS_TOO_MANY_OPENED        -7
#define     K_ERR_FS_TOO_MANY_FILES         -8
#define     K_ERR_FS_FILE_NOT_FOUND         -9
#define     K_ERR_FS_FILE_ALREADY_OPENED    -10
#define     K_ERR_FS_FILE_TOO_LARGE         -11
#define     K_ERR_FS_READ_ERROR             -12
#define     K_ERR_FS_WRITE_ERROR            -13
#define     K_ERR_FS_DEVICE_FULL            -14
#define     K_ERR_FS_ENDIAN_ERROR           -15
#define     K_ERR_FS_TOO_FEW_BLOCKS         -16
#define     K_ERR_FS_MEMORY_ALLOCATE        -17
#define     K_ERR_FS_FILE_LOCKED            -18
#define     K_ERR_FS_PATH_NOT_FOUND         -19
#define     K_ERR_FS_DIR_NOT_EMPTY          -20
#define     K_ERR_FS_NOT_SUPPORT            -21

//------------------------------------------------------------------------------
//      Error code for device
//------------------------------------------------------------------------------
enum error_code_for_device
{
	EINVAL = 51, // Invalid argument
	ENODEV,      // No such device
	ENOENT,      // No such file or directory
	EINPROGRESS, // Operation now in progress
	ENOEXEC,     // Exec format error
	EMSGSIZE,    // Message too long
	ENOTCONN,    // Transport end point is not connected
	EBUSY,       // Device ot rsource busy
	EPERM,       // Operation not permitted
	EOPNOTSUPP,  // OPeration not supported on transport endpoint
	EAGAIN,      // Try again
	ETIMEDOUT,   // Connection timed out
	ENOMEM,      // Out of memory
	EPROTO,      // Protocol Error
	EPIPE,       // Broken pipe
	EHOSTUNREACH,// No route to host
	ESHUTDOWN,   // Cannot send after transport endpoint shutdown
	EIDRM,       // Identifier removed
	ENOTSUPP,    // Operation not supported on socket
	EEXIST,      // File already exists
	ECONNRESET,  // Connection reset by perr
	EIO,         // I/O error
	EISDIR,      // Is a directory
	EOVERFLOW,   // Value too large for defined data type
	EILSEQ,      // Illegal byte sequence
	EREMOTEIO,   // Remote I/O error
	ECOMM,       // Communication error
	ENOSR,       // Out of streams resources
	EALREADY,    // Operation already in progress
	ETIME        // Timer expired
};
//==========================================================================

// File Open mode bits
#define K_O_RDONLY              0x0000
#define K_O_WRONLY              0x0001
#define K_O_RDWR                0x0002
#define K_O_CREAT               0x0040
#define K_O_TRUNC               0x0200
#define K_O_APPEND              0x0400
#define K_O_SHARE		0x0800

// bit definitions of file attributes
#define K_FILE_OTH_WRITE        0x02    // others have write permission
#define K_FILE_OTH_READ         0x04    // others have read permission
#define K_FILE_USR_WRITE        0x10    // user has write permission
#define K_FILE_USR_READ         0x20    // user has read permission
#define K_FILE_ACTIVE           0x40    // active
#define K_FILE_VIRGIN           0x80    // virgin flags
#define K_FILE_EXECUTABLE       0x100   // this is a executable file
#define K_FILE_ENCRYPTED        0x200   // file encrypted
#define K_FILE_LOCKED           0x400   // indicate file is locked by s_auth_key
#define K_FILE_SYSTEM           0x800   // indicate system file create is requested when created with owner
#define K_FILE_DIRECTORY        0x1000  // directory
#define K_DIR_CREATE            0x2000  // allows create file in this dir
#define K_DIR_DELETE            0x4000  // allows delete file in this dir

// for file seek
#define K_FilePosHome           0
#define K_FileCurrPos           (DWORD)-2
#define K_FilePosEnd            (DWORD)-1

// file search control for search and dsearch
// for dsearch, only owner file will be returned
#define K_SearchNext            0
#define K_SearchReset           1

// use for search only, dsearch has no effect
#define K_SearchAll             0x1e
#define K_SearchData            0x02
#define K_SearchApp             0x04
#define K_SearchActivate        0x08
#define K_SearchDeactivate      0x10

// for dsearch only
#define K_SearchPub             2

#define K_FlushAll              ((DWORD)-1)

// T_FSINFO constants
// device type (bit0-7 of d_device)
#define K_FSDEV_NAND            1
#define K_FSDEV_NOR             2
#define K_FSDEV_DATAFLASH       3
#define K_FSDEV_USB             11
#define K_FSDEV_SD              12
#define K_FSDEV_REMOVEABLE      0x100
#define K_FSDEV_READONLY        0x200

// file system format ID
#define K_FSFORMAT_YAFFS        1
#define K_FSFORMAT_FAT32        2
#define K_FSFORMAT_FAT16        3

// file system information
typedef struct {
  BYTE b_devname[32];
  BYTE b_device;
  BYTE b_format;
  BOOLEAN b_case_sensitive;
  BYTE rfu;
  DWORD d_version;
  DWORD d_capacity;
  DWORD d_sectorsize;
} T_FSINFO;

typedef struct {
  BYTE  s_name[20];
  union {
    BYTE  s_owner[11];
    BYTE  s_agent_id[8];
  };
  DWORD d_attr;
  DWORD d_filelen;
  BYTE  s_create_date[7];
  BYTE  s_last_update[7];
} T_FILE;

typedef struct {
  BYTE  s_name[256];
  BYTE  s_owner[16];
  DWORD d_attr;
  DWORD d_filelen;
  DWORD d_occupied_size;
  BYTE  s_create_date[7];
  BYTE  rfu1;
  BYTE  s_last_update[7];
  BYTE  rfu2;
} T_DirEntry;

typedef struct
{
  DWORD marker;           // valid mark
  int d_index;            // index of next entry
  void *v_dirObj;         // pointer to directory object
  void *v_nextEntry;      // pointer to next entry
  void *v_fs;             // pointer to file system
  DWORD magic;            // magic code
  T_DirEntry entry;
} T_Dir;

#define   OS_FN_FILE_CREATE_OWNER (OS_FN_FREE + 1)
// int os_file_create_with_owner(BYTE * a_name,DWORD a_attribut,BYTE * a_owner); // system use only
#define os_file_create_with_owner(x,y,z) ((int)sys_func3(OS_FN_FILE_CREATE_OWNER,(DWORD)(x),(DWORD)(y),(DWORD)(z)))

#define   OS_FN_FILE_OPEN       (OS_FN_FILE_CREATE_OWNER + 1)
// int os_file_open(BYTE * a_name,DWORD a_mode);
#define os_file_open(x,y)       ((int)sys_func2(OS_FN_FILE_OPEN,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_CREATE     (OS_FN_FILE_OPEN + 1)
// int os_file_create(BYTE * a_name,DWORD a_attribut);
#define os_file_create(x,y)     ((int)sys_func2(OS_FN_FILE_CREATE,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_CLOSE      (OS_FN_FILE_CREATE + 1)
// int os_file_close(DWORD a_fid);
#define os_file_close(x)       sys_func1(OS_FN_FILE_CLOSE,(DWORD)(x))

#define   OS_FN_FILE_EXIST      (OS_FN_FILE_CLOSE + 1)
// BOOLEAN os_file_exist(BYTE * a_name);
#define os_file_exist(x)        sys_func1(OS_FN_FILE_EXIST,(DWORD)(x))

#define   OS_FN_FILE_RENAME     (OS_FN_FILE_EXIST + 1)
// int os_file_rename(BYTE * a_newname, BYTE * a_oldname);
#define os_file_rename(x,y)     sys_func2(OS_FN_FILE_RENAME,(DWORD)(x),(DWORD)(y))

#define   OS_FN_FILE_DELETE     (OS_FN_FILE_RENAME + 1)
// int os_file_delete(BYTE * a_name);
#define os_file_delete(x)       sys_func1(OS_FN_FILE_DELETE,(DWORD)(x))

#define   OS_FN_FILE_FLUSHED    (OS_FN_FILE_DELETE + 1)
// BOOLEAN os_file_flushed(DWORD a_fid);
#define os_file_flushed(x)      sys_func1(OS_FN_FILE_FLUSHED,(DWORD)(x))

#define   OS_FN_FILE_SEEK       (OS_FN_FILE_FLUSHED + 1)
// int os_file_seek(DWORD a_fid,DWORD a_pos);
#define os_file_seek(x,y)       ((int)sys_func2(OS_FN_FILE_SEEK,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_READ       (OS_FN_FILE_SEEK + 1)
// int os_file_read(DWORD a_fid,BYTE * a_buffer,DWORD a_len);
#define os_file_read(x,y,z)     ((int)sys_func3(OS_FN_FILE_READ,(DWORD)(x),(DWORD)(y),(DWORD)(z)))

#define   OS_FN_FILE_WRITE      (OS_FN_FILE_READ + 1)
// int os_file_write(DWORD a_fid,BYTE * a_buffer,DWORD a_len);
#define os_file_write(x,y,z)    ((int)sys_func3(OS_FN_FILE_WRITE,(DWORD)(x),(DWORD)(y),(DWORD)(z)))

#define   OS_FN_FILE_PUTC       (OS_FN_FILE_WRITE + 1)
// int os_file_putc(DWORD a_fid,DWORD c);
#define os_file_putc(x,y)       ((int)sys_func2(OS_FN_FILE_PUTC,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_GETC       (OS_FN_FILE_PUTC + 1)
// int os_file_getc(DWORD a_fid);
#define os_file_getc(x)         ((int)sys_func1(OS_FN_FILE_GETC,(DWORD)(x)))

#define   OS_FN_FILE_LENGTH     (OS_FN_FILE_GETC + 1)
// int os_file_length(DWORD a_fid);
#define os_file_length(x)       ((int)sys_func1(OS_FN_FILE_LENGTH,(DWORD)(x)))

#define   OS_FN_FILE_END        (OS_FN_FILE_LENGTH + 1)
// BOOLEAN os_file_end(DWORD a_fid);
#define os_file_end(x)          sys_func1(OS_FN_FILE_END,(DWORD)(x))

#define   OS_FN_FILE_SEARCH     (OS_FN_FILE_END + 1)
//T_FILE * os_file_search(DWORD a_control) // system only
#define os_file_search(x)       (T_FILE *)sys_func1(OS_FN_FILE_SEARCH,(DWORD)(x))

#define   OS_FN_FILE_DSEARCH    (OS_FN_FILE_SEARCH + 1)
//T_FILE * os_file_search_data(DWORD a_control, T_FILE *a_file)
#define os_file_search_data(x,y)  (T_FILE *)sys_func2(OS_FN_FILE_DSEARCH,(DWORD)(x), (DWORD)(y))

#define   OS_FN_FILE_DEACTIVATE (OS_FN_FILE_DSEARCH + 1)
//BOOLEAN os_file_deactivate(BYTE * a_name)
#define os_file_deactivate(x)   sys_func1(OS_FN_FILE_DEACTIVATE,(DWORD)(x))

#define   OS_FN_FILE_ACTIVATE   (OS_FN_FILE_DEACTIVATE + 1)
//BOOLEAN os_file_activate(BYTE * a_name)
#define os_file_activate(x)     sys_func1(OS_FN_FILE_ACTIVATE,(DWORD)(x))

#define   OS_FN_FILE_GET_NAME   (OS_FN_FILE_ACTIVATE + 1)
//int os_file_get_name(DWORD a_fid,BYTE * a_name)
#define os_file_get_name(x,y)   ((int)sys_func2(OS_FN_FILE_GET_NAME,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_TRUNCATE   (OS_FN_FILE_GET_NAME + 1)
//int os_file_truncate(BYTE * a_name,DWORD a_len)
#define os_file_truncate(x,y)   ((int)sys_func2(OS_FN_FILE_TRUNCATE,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_AUTH       (OS_FN_FILE_TRUNCATE + 1)
//int os_file_auth(DWORD a_fid, BYTE *a_hash)
#define os_file_auth(x,y)       ((int)sys_func2(OS_FN_FILE_AUTH,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_SET_LOCK   (OS_FN_FILE_AUTH + 1)
//int os_file_set_lock(DWORD a_fid, BYTE *a_key)
#define os_file_set_lock(x,y)   ((int)sys_func2(OS_FN_FILE_SET_LOCK,(DWORD)(x),(DWORD)(y)))

#define   OS_FN_FILE_GET_FREE_SPACE (OS_FN_FILE_SET_LOCK + 1)
//unsigned int os_file_get_free_space()
#define os_file_get_free_space() (sys_func0(OS_FN_FILE_GET_FREE_SPACE))

                /****** config routine ******/

#define   K_CF1_End           K_CF_Netmask
#define   K_CF_Version        0                   // system only
#define   K_CF_DebugMode      K_CF_Version+1      // system only
#define   K_CF_Rtc            K_CF_DebugMode+1    // system only
#define   K_CF_SerialNo       K_CF_Rtc+1          // read only
#define   K_CF_Tid            K_CF_SerialNo+1     // read only
#define   K_CF_AppKekKvc      K_CF_Tid+1          // system only
#define   K_CF_EtherID        K_CF_AppKekKvc+1    // read only
#define   K_CF_PppFixIP       K_CF_EtherID+1      // r/w
#define   K_CF_IP             K_CF_PppFixIP+1     // r/w
#define   K_CF_Netmask        K_CF_IP+1           // r/w
#define   K_CF_Gateway        K_CF_Netmask+1      // r/w
#define   K_CF_DNS1           K_CF_Gateway+1      // r/w
#define   K_CF_DNS2           K_CF_DNS1+1         // r/w
#define   K_CF_UDPPort        K_CF_DNS2+1         // r/w
#define   K_CF_TCPPort        K_CF_UDPPort+1      // r/w
#define   K_CF_DHCPEnable     K_CF_TCPPort+1      // r/w
#define   K_CF_MdmCountry     K_CF_DHCPEnable+1   // r/w, not use
#define   K_CF_MdmTxPwr       K_CF_MdmCountry+1   // r/w, not use
#define   K_CF_MdmDTPwr       K_CF_MdmTxPwr+1     // r/w, not use
#define   K_CF_MdmDTMFPwr     K_CF_MdmDTPwr+1     // r/w, not use
#define   K_CF_MdmU80         K_CF_MdmDTMFPwr+1   // r/w, not use
#define   K_CF_Logo           K_CF_MdmU80+1       // write only
#define   K_CF_TamperOn       K_CF_Logo+1         // system only
#define   K_CF_BSysPsw        K_CF_TamperOn+1     // system only
#define   K_CF_DllPsw         K_CF_BSysPsw+1      // system only
#define   K_CF_SetupPsw       K_CF_DllPsw+1       // system only
#define   K_CF_TamperPsw      K_CF_SetupPsw+1     // system only
#define   K_CF_EnforcePsw     K_CF_TamperPsw+1    // system only
#define   K_CF_DMK            K_CF_EnforcePsw+1   // system only
#define   K_CF_Tampered       K_CF_DMK+1          // r/w
#define   K_CF_TimeStamp      K_CF_Tampered+1     // system only
#define   K_CF_ParsePhrase    K_CF_TimeStamp+1    // system only
#define   K_CF_CurRdllInf     K_CF_ParsePhrase+1  // system only
#define   K_CF_SysRdllInf     K_CF_CurRdllInf+1   // system only
#define   K_CF_RdllInf        K_CF_SysRdllInf+1   // r/w
#define   K_CF_HwConfig       K_CF_RdllInf+1      // read only, that is product code
#define   K_CF_LcdBrightness  K_CF_HwConfig+1     // read only
#define   K_CF_LcdTimeout     K_CF_LcdBrightness+1// read only
#define   K_CF_SimSelect      K_CF_LcdTimeout+1   // r/w
#define   K_CF_IncomingCall   K_CF_SimSelect+1    // r/w
#define   K_CF_BattChgMode    K_CF_IncomingCall+1 // r/w
#define   K_CF_BattError      K_CF_BattChgMode+1  // r/w
#define   K_CF_LcdBOffset     K_CF_BattError+1    // system only
#define   K_CF_KbdBL          K_CF_LcdBOffset+1   // r/w
#define   K_CF_SmsEnable      K_CF_KbdBL+1        // read only
#define   K_CF_TiltParam      K_CF_SmsEnable+1    // read only
#define   K_CF_PN532_CF_02    K_CF_TiltParam+1    // read only
#define   K_CF_PN532_CF_04    K_CF_PN532_CF_02+1  // read only
#define   K_CF_PN532_CF_05    K_CF_PN532_CF_04+1  // read only
#define   K_CF_PN532_CF_0A    K_CF_PN532_CF_05+1  // read only
#define   K_CF_PN532_CF_0B    K_CF_PN532_CF_0A+1  // read only
#define   K_CF_PN532_CF_0C    K_CF_PN532_CF_0B+1  // read only
#define   K_CF_PN532_CF_0D    K_CF_PN532_CF_0C+1  // read only
#define   K_CF_ATDebounce     K_CF_PN532_CF_0D+1  // r/w
#define   K_CF_Max            K_CF_ATDebounce+1

#define   K_ProdCodeMax       30

// Battery charging mode
#define   K_BattLongLifeMode  1
#define   K_BattHighPerMode   0

// Battery error status
#define   K_BattOk            0
#define   K_BattPreChgError   1
#define   K_BattTempTimeError 2


#define   OS_FN_CONFIG_LENGTH  (OS_FN_FILE_GET_FREE_SPACE + 1)
// DWORD config_length(DWORD a_idx);
#define os_config_length(x)    sys_func1(OS_FN_CONFIG_LENGTH,(DWORD)(x))

#define   OS_FN_CONFIG_READ    (OS_FN_CONFIG_LENGTH + 1)
// BOOLEAN config_read(DWORD a_idx,BYTE * a_dest);
#define os_config_read(x,y)    sys_func2(OS_FN_CONFIG_READ,(DWORD)(x),(DWORD)(y))

#define   OS_FN_CONFIG_WRITE   (OS_FN_CONFIG_READ + 1)
// BOOLEAN config_write(DWORD a_idx,BYTE * a_src);
#define os_config_write(x,y)   sys_func2(OS_FN_CONFIG_WRITE,(DWORD)(x),(DWORD)(y))

#define   OS_FN_CONFIG_UPDATE  (OS_FN_CONFIG_WRITE + 1)
// BOOLEAN config_update(void);
#define os_config_update()     sys_func0(OS_FN_CONFIG_UPDATE)

                /****** sam routine ******/

#define   OS_FN_SAM_OPEN       (OS_FN_CONFIG_UPDATE + 1)
// void os_sam_open(void);
#define os_sam_open()          sys_func0(OS_FN_SAM_OPEN)

#define   OS_FN_SAM_CLOSE      (OS_FN_SAM_OPEN + 1)
// void os_sam_close(void);
#define os_sam_close()         sys_func0(OS_FN_SAM_CLOSE)

#define   OS_FN_SAM_SELECT     (OS_FN_SAM_CLOSE + 1)
// void os_sam_select(DWORD a_index_mode);
#define os_sam_select(x)       sys_func1(OS_FN_SAM_SELECT,(DWORD)(x))

#define   OS_FN_SAM_INSERTED   (OS_FN_SAM_SELECT + 1)
// BOOLEAN os_sam_inserted(DWORD a_index);
#define os_sam_inserted(x)     sys_func1(OS_FN_SAM_INSERTED,(DWORD)(x))

#define   OS_FN_SAM_ON         (OS_FN_SAM_INSERTED + 1)
// DWORD os_sam_on(BYTE * a_history);
#define os_sam_on(x)           sys_func1(OS_FN_SAM_ON,(DWORD)(x))

#define   OS_FN_SAM_OFF        (OS_FN_SAM_ON + 1)
// void os_sam_off(void);
#define os_sam_off()           sys_func0(OS_FN_SAM_OFF)

#define   OS_FN_SAM_T1         (OS_FN_SAM_OFF + 1)
// BOOLEAN os_sam_t1(T_T1 * a_ptr);
#define os_sam_t1(x)           sys_func1(OS_FN_SAM_T1,(DWORD)(x))

#define   OS_FN_SAM_CHANGE     (OS_FN_SAM_T1 + 1)
// BOOLEAN os_sam_change(void);
#define os_sam_change()        sys_func0(OS_FN_SAM_CHANGE)

                /****** display routine 3 ******/

#define   OS_FN_DISP_PUTP      (OS_FN_SAM_CHANGE + 1)
// DWORD os_disp_putp(DWORD a_pos); bit 0-7 column, bit 8-15 row
#define os_disp_putp(x)        sys_func1(OS_FN_DISP_PUTP,(DWORD)(x))

#define   OS_FN_DISP_GETP      (OS_FN_DISP_PUTP + 1)
// DWORD os_disp_getp(void);
#define os_disp_getp()         sys_func0(OS_FN_DISP_GETP)

#define   OS_FN_DISP_BL_CONTROL (OS_FN_DISP_GETP + 1)
// DWORD os_disp_bl_control(DWORD a_time); // 0 = off now, 0xff = on now
#define os_disp_bl_control(x)   sys_func1(OS_FN_DISP_BL_CONTROL,(DWORD)(x))

#define K_BattChargingColor     0x1F    // red is reserved to indicate battery charging
#define   OS_FN_DISP_ICON_COLOR (OS_FN_DISP_BL_CONTROL + 1)
// void os_disp_icon_color(T_DISP_ICON_COLOR  *a_icon_color);
#define os_disp_icon_color(x)   sys_func1(OS_FN_DISP_ICON_COLOR,(DWORD)(x))

                /****** auxdbg routine 2 ******/

#define   OS_FN_AUXDBG_SREAD     (OS_FN_DISP_ICON_COLOR + 1)
// T_BUF * os_auxdbg_sread(void);  // system only
#define os_auxdbg_sread()        ((T_BUF *)sys_func0(OS_FN_AUXDBG_SREAD))

#define   OS_FN_AUXDBG_SSEND     (OS_FN_AUXDBG_SREAD + 1)
// BOOLEAN os_auxdbg_ssend(T_BUF * a_ptr); // system only
#define os_auxdbg_ssend(x)       sys_func1(OS_FN_AUXDBG_SSEND,(DWORD)(x))

                /****** encryption routine 2 ******/

#define   OS_FN_RSA_PRIVATE       (OS_FN_AUXDBG_SSEND + 1)
//void os_rsa_private(BYTE *a_msg,T_RSA_PRIVATE_KEY *a_private_key);
#define os_rsa_private(x,y)       sys_func2(OS_FN_RSA_PRIVATE,(DWORD)(x),(DWORD)(y))

                /****** HSM routine 2 ******/

#define   OS_FN_KEY_RSA          (OS_FN_RSA_PRIVATE + 1)
// void os_key_rsa(BYTE *a_data,T_KEY *a_ekey);
#define os_key_rsa(x,y)          sys_func2(OS_FN_KEY_RSA,(DWORD)(x),(DWORD)(y))

#define   OS_FN_KEY_RSA_PRIVATE  (OS_FN_KEY_RSA + 1)
// void os_key_rsa_private(BYTE *a_data,T_RSA_PRIVATE_KEY *a_private_ekey);
#define os_key_rsa_private(x,y)  sys_func2(OS_FN_KEY_RSA_PRIVATE,(DWORD)(x),(DWORD)(y))

                /****** File routine 2 ******/

#define   OS_FN_FILE_FREESPACE  (OS_FN_KEY_RSA_PRIVATE + 1)
//unsigned int os_file_freeSpace(BYTE *path)
#define os_file_freeSpace(x)  (sys_func1(OS_FN_FILE_FREESPACE, (DWORD)(x)))

#define   OS_FN_FILE_GET_SYSTEMINFO   (OS_FN_FILE_FREESPACE + 1)
//int os_file_get_systeminfo(const char *path, T_FSINFO *fsinfo);
#define os_file_systeminfo(x,y) (int)(sys_func2(OS_FN_FILE_GET_SYSTEMINFO, (DWORD)(x), (DWORD)(y)))

#define   OS_FN_FILE_FTRUNCATE  (OS_FN_FILE_GET_SYSTEMINFO + 1)
//int os_file_ftruncate(DWORD a_fid, DWORD a_len);
#define os_file_ftruncate(x,y)  (sys_func2(OS_FN_FILE_FTRUNCATE, (DWORD)(x), (DWORD)(y)))

#define   OS_FN_FILE_INIT_SEARCH  (OS_FN_FILE_FTRUNCATE + 1)
//int os_file_init_search(const char *path, T_Dir *dir);
#define os_file_init_search(x,y)  (int)(sys_func2(OS_FN_FILE_INIT_SEARCH, (DWORD)(x), (DWORD)(y)))

#define   OS_FN_FILE_READDIR  (OS_FN_FILE_INIT_SEARCH + 1)
//T_DirEntry *os_file_readdir(T_Dir *dir);
#define os_file_readdir(x)  (sys_func1(OS_FN_FILE_READDIR, (DWORD)(x)))

#define   OS_FN_FILE_MKDIR  (OS_FN_FILE_READDIR + 1)
//int os_file_mkdir(const char *path, unsigned int attr);
#define os_file_mkdir(x,y)  (int)(sys_func2(OS_FN_FILE_MKDIR, (DWORD)(x), (DWORD)(y)))

#define   OS_FN_FILE_RMDIR  (OS_FN_FILE_MKDIR + 1)
//int os_file_rmdir(const char *path);
#define os_file_rmdir(x)  (int)(sys_func1(OS_FN_FILE_RMDIR, (DWORD)(x)))

#define   OS_FN_FILE_GET_LONGNAME   (OS_FN_FILE_RMDIR + 1)
//int os_file_get_longname(DWORD a_fid, BYTE *a_name, DWORD a_maxlen);
#define os_file_get_longname(x,y,z) (int)(sys_func3(OS_FN_FILE_GET_LONGNAME, (DWORD)(x), (DWORD)(y), (DWORD)(z)))

                /****** mmc routine ******/
#define K_MMCOpenError          ((DWORD)-1)
#define K_NoCard                0
#define K_MMC                   1
#define K_SD                    2         // < 4G
#define K_SDHC                  3         // up to 4T

#define   OS_FN_MMC_OPEN       (OS_FN_FILE_GET_LONGNAME + 1)
// BOOLEAN os_mmc_open(void);
#define os_mmc_open()          sys_func0(OS_FN_MMC_OPEN)

#define   OS_FN_MMC_CLOSE      (OS_FN_MMC_OPEN + 1)
// void os_mmc_close(void);
#define os_mmc_close()         sys_func0(OS_FN_MMC_CLOSE)

#define   OS_FN_MMC_BLK_SIZE   (OS_FN_MMC_CLOSE + 1)
// WORD os_mmc_block_size(void);
#define os_mmc_block_size()    sys_func0(OS_FN_MMC_BLK_SIZE)

#define   OS_FN_MMC_SIZE       (OS_FN_MMC_BLK_SIZE + 1)
// DWORD os_mmc_size(void); // in unit 512-byte or block size
#define os_mmc_size()          sys_func0(OS_FN_MMC_SIZE)

#define   OS_FN_MMC_PSTATUS    (OS_FN_MMC_SIZE + 1)
// BOOLEAN os_mmc_pstatus(void);
#define os_mmc_pstatus()       sys_func0(OS_FN_MMC_PSTATUS)

#define   OS_FN_MMC_BLK_READ   (OS_FN_MMC_PSTATUS + 1)
// BOOLEAN os_mmc_block_read(T_MMC * a_ptr);
#define os_mmc_block_read(x)   sys_func1(OS_FN_MMC_BLK_READ,(DWORD)(x))

#define   OS_FN_MMC_BLK_WRITE  (OS_FN_MMC_BLK_READ + 1)
// BOOLEAN os_mmc_block_write(T_MMC * a_ptr);
#define os_mmc_block_write(x)  sys_func1(OS_FN_MMC_BLK_WRITE,(DWORD)(x))

#define   OS_FN_MMC_READ       (OS_FN_MMC_BLK_WRITE + 1)
// BOOLEAN os_mmc_read(T_MMC * a_ptr,DWORD a_len);
#define os_mmc_read(x,y)       sys_func2(OS_FN_MMC_READ,(DWORD)(x),(DWORD)(y))

#define   OS_FN_MMC_WRITE      (OS_FN_MMC_READ + 1)
// BOOLEAN os_mmc_write(T_MMC * a_ptr,DWORD a_len);
#define os_mmc_write(x,y)      sys_func2(OS_FN_MMC_WRITE,(DWORD)(x),(DWORD)(y))

#define   OS_FN_MMC_INSERTED   (OS_FN_MMC_WRITE + 1)
// BOOLEAN os_mmc_inserted(void);
#define os_mmc_inserted()      sys_func0(OS_FN_MMC_INSERTED)

                /****** sle routine ******/

/* SLE type return by os_sle_open() */
#define K_TypeNone              0
#define K_TypeSLE4442           1
#define K_TypeSLE4428           2
#define K_TypeSLE4406           3
#define K_TypeAT88SC153         4

/* Siemens memory card AT88SC153 */
#define K_Cmd153VerifyPassword    0x30
#define K_Cmd153RdBytes           0x31
#define K_Cmd153RdFuse            0x32
#define K_Cmd153WdBytes           0x33
#define K_Cmd153WdFuse            0x34
#define K_Cmd153Auth              0x35
#define K_Cmd153AtmelF2Init       0x36
#define K_Cmd153AtmelF2           0x37

/* Siemens memory card SLE4406 */
#define K_Cmd06WdBit              0x30
#define K_Cmd06WdBits             0x31
#define K_Cmd06WdByte             0x32
#define K_Cmd06WdBytes            0x33
#define K_Cmd06RdBits             0x34
#define K_Cmd06RdBytes            0x35
#define K_Cmd06RdCount            0x36
#define K_Cmd06DecCount           0x37
#define K_Cmd06SubCount           0x38
#define K_Cmd06PscVerify          0x39
#define K_Cmd06Personalize        0x40

/* Siemens memory card SLE6636 */
#define K_Cmd36WdBit              0x30
#define K_Cmd36WdBits             0x31
#define K_Cmd36WdByte             0x32
#define K_Cmd36WdBytes            0x33
#define K_Cmd36RdBits             0x34
#define K_Cmd36RdBytes            0x35
#define K_Cmd36RdCount            0x36
#define K_Cmd36DedCount           0x37
#define K_Cmd36SubCount           0x38
#define K_Cmd36Auth               0x39

/* Siemens memory card SLE4442 */
#define K_Cmd42RdMainMem          0x30
#define K_Cmd42UpMainMem          0x38
#define K_Cmd42RdProtMem          0x34
#define K_Cmd42WrProtMem          0x3C
#define K_Cmd42RdSecuMem          0x31
#define K_Cmd42UpSecuMem          0x39
#define K_Cmd42PscVerify          0x33

/* Siemens memory card SLE4428 */
#define K_Cmd28Rd8B               0x0E
#define K_Cmd28Rd9B               0x0C
#define K_Cmd28Wr8B               0x33
#define K_Cmd28Wr9B               0x31
#define K_Cmd28PscVerify          0x30

#define   OS_FN_SLE_OPEN                (OS_FN_MMC_INSERTED + 1)
// DWORD os_sle_open(void);
#define os_sle_open()           sys_func0(OS_FN_SLE_OPEN)

#define   OS_FN_SLE_CLOSE       (OS_FN_SLE_OPEN + 1)
// void os_sle_close(void);
#define os_sle_close()          sys_func0(OS_FN_SLE_CLOSE)

#define   OS_FN_SLE_HIST        (OS_FN_SLE_CLOSE + 1)
// void os_sle_hist(BYTE * hist);
#define os_sle_hist(x)          sys_func1(OS_FN_SLE_HIST,(DWORD)(x))

#define   OS_FN_SLE_IO          (OS_FN_SLE_HIST + 1)
// BOOLEAN os_sle_io(T_T0 * a_ptr);
#define os_sle_io(x)            sys_func1(OS_FN_SLE_IO,(DWORD)(x))

                /****** ethernet routine system only ******/

#define   OS_FN_LAN_OPEN        (OS_FN_SLE_IO + 1)
//BOOLEAN os_lan_open(void);
#define os_lan_open()           sys_func0(OS_FN_LAN_OPEN)

#define   OS_FN_LAN_CLOSE       (OS_FN_LAN_OPEN + 1)
//void os_lan_close(void);
#define os_lan_close()          sys_func0(OS_FN_LAN_CLOSE)

#define   OS_FN_LAN_RXRDY       (OS_FN_LAN_CLOSE + 1)
//BOOLEAN os_lan_rxrdy(void);
#define os_lan_rxrdy()          sys_func0(OS_FN_LAN_RXRDY)

#define   OS_FN_LAN_TXRDY       (OS_FN_LAN_RXRDY + 1)
//BOOLEAN os_lan_txrdy(void);
#define os_lan_txrdy()          sys_func0(OS_FN_LAN_TXRDY)

#define   OS_FN_LAN_READ        (OS_FN_LAN_TXRDY + 1)
//T_BUF * os_lan_read();
#define os_lan_read()           ((T_BUF *)sys_func0(OS_FN_LAN_READ))

#define   OS_FN_LAN_SEND        (OS_FN_LAN_READ + 1)
//BOOLEAN os_lan_send(void * a_ptr);
#define os_lan_send(x)          sys_func1(OS_FN_LAN_SEND,(DWORD)(x))

#define   OS_FN_LAN_STATUS      (OS_FN_LAN_SEND + 1)
//DWORD os_lan_status(DWORD a_select);
//a_select = 0 => read and clear status
//a_select = 1 => read status only
#define K_LanRCStatus       0
#define K_LanRStatus        1
#define os_lan_status(x)        sys_func1(OS_FN_LAN_STATUS,(x))

typedef struct {
  DWORD d_Overflow;
  DWORD d_InErrLen;
  DWORD d_InDiscards;
  DWORD d_FrameAlignment;
  DWORD d_PhyLayerError;
  DWORD d_TxError;
} T_LAN_STAT;

#define   OS_FN_LAN_STATISTIC   (OS_FN_LAN_STATUS + 1)
//void os_lan_statistic(T_LAN_STAT *a_pstatistic);
#define os_lan_statistic(x)     sys_func1(OS_FN_LAN_STATISTIC,(x))

                /****** msr routine ******/

#define K_MsrReset              0x80
#define K_AllTracks             0x07
#define K_Track2                0x01
#define K_Track1                (K_Track2 << 1)
#define K_Track3                (K_Track2 << 2)
#define K_Trk1BufLen            158 // 79
#define K_Trk2BufLen            80  // 40
#define K_Trk3BufLen            214 // 107

#define K_T2Ok                  (K_Track2)
#define K_T2Error               (K_Track2 << 8)
#define K_T1Ok                  (K_Track1)
#define K_T1Error               (K_Track1 << 8)
#define K_T3Ok                  (K_Track3)
#define K_T3Error               (K_Track3 << 8)

#define   OS_FN_MSR_OPEN        (OS_FN_LAN_STATISTIC + 1)
/*
  BYTE  b_len;
  BYTE  b_msr_mode;   (BIT 0 = TRK 2; BIT 1 = TRK 1; BIT 2 = TRK 3)
  BYTE  b_x1;         qalify time in step of 5 bytes. 0-4 and default is 0
                      qualify byte from 10-30 bytes
  BYTE  b_x2;         (not used)
  WORD  w_buffer_size;((K_Trk1BufLen+1) + (K_Trk2BufLen+1)
                       + (K_Trk3BufLen+1) + max(BUFLEN + 1)) (not used)
  BYTE   * p_buffer_address; (not used)
*/
// BOOLEAN os_msr_open(void * a_init_str);
#define os_msr_open(x)          sys_func1(OS_FN_MSR_OPEN,(DWORD)(x))

#define   OS_FN_MSR_CLOSE       (OS_FN_MSR_OPEN + 1)
// void os_msr_close(void);
#define os_msr_close()          sys_func0(OS_FN_MSR_CLOSE)

#define   OS_FN_MSR_RXLEN       (OS_FN_MSR_CLOSE + 1)
// WORD os_msr_rxlen(void);
#define os_msr_rxlen()          sys_func0(OS_FN_MSR_RXLEN)

#define   OS_FN_MSR_GETC        (OS_FN_MSR_RXLEN + 1)
// BYTE os_msr_getc(void);
#define os_msr_getc()           sys_func0(OS_FN_MSR_GETC)

#define   OS_FN_MSR_READ        (OS_FN_MSR_GETC + 1)
// WORD os_msr_read(void * a_ptr,DWORD a_max_len);
#define os_msr_read(x,y)        sys_func2(OS_FN_MSR_READ,(DWORD)(x),(DWORD)(y))

#define   OS_FN_MSR_STATUS      (OS_FN_MSR_READ + 1)
/*
  bit0 - 1 => track 2 ok
  bit1 - 1 => track 1 ok
  bit2 - 1 => track 3 ok
  bit8 - 1 => track 2 error and start sentinel found
  bit9 - 1 => track 1 error and start sentinel found
  bit10- 1 => track 3 error and start sentinel found
*/
// WORD os_msr_status(DWORD a_select);
#define os_msr_status(x)        sys_func1(OS_FN_MSR_STATUS,(DWORD)(x))

                /****** printer rountine ******/

/* printer status define */
#define K_LptOk                 0
#define K_ErrTempHigh           1
#define K_ErrVoltLow            2
#define K_ErrPaperout           3
#define K_LptTempHigh           0x100
#define K_LptPaperout           0x200
#define K_LptLowVolt            0x400
#define K_LptPrinting           0x800
#define K_LptFinish             0x1000

#define K_LptNormalMode         0
#define K_LptBarcodeMode        1
#define   OS_FN_LPT_OPEN        (OS_FN_MSR_STATUS + 1)
// a_init_str
//    byte 0 = data length follow,5
//    byte 1 = mode
//                = 0, normal mode and bytes 2-5 are used to control strobe and print speed
//                = 1, barcode mode, byte 2 is started at 2 and byte 3 is started at 3, bytes 4 & 5 are still applied.
//    byte 2 = print speed when power in, 0-4, 0 = 22 lines/sec, .. , and 4 = 14 lines/sec
//    byte 3 = print speed in battery mode, 0-4, 0 = 16 lines/sec,.., and 4 = 8 lines/sec
//    byte 4 = power in strobe width adjustment, sign byte +/- 0-7, +ve = increase strobe width
//    byte 5 = battery mode strobe width adjustment, sign byte +/- 0-7, +ve = increase strobe width
// BOOLEAN os_lpt_open(BYTE * a_init_str);
#define os_lpt_open(x)          sys_func1(OS_FN_LPT_OPEN,(DWORD)(x))

#define   OS_FN_LPT_CLOSE       (OS_FN_LPT_OPEN + 1)
// void os_lpt_close(void);
#define os_lpt_close()          sys_func0(OS_FN_LPT_CLOSE)

#define   OS_FN_LPT_PUTC        (OS_FN_LPT_CLOSE + 1)
/*
  ESC B 0        - bold off
  ESC B 1        - bold on
  ESC U 0        - underline off
  ESC U 1        - underline on
  ESC W 0        - double width off
  ESC W 1        - double width on
  ESC H 0        - double height off
  ESC H 1        - double height on
  ESC F n        - select font type
                   0 = select 16 * 12 English
                   1 = select 16 * 8 English
                   2 = select 16(H) x 12(W) English-Enlarged (32 char/line)
                   3 = select special small font of default size (8W*16H)
                   4 = select special normal font, size returned by  SPFONT
                   5 = select special normal T font size returned by SPFONT
                   6 = select special normal SP font size returned by SPFONT
  ESC J n        - feed paper n lines
  ESC K x y data - print graphic with x is hieght in lines and y is width in byte
                   data = x * y bytes
  ESC @          - reset printer
  ESC D n        - set half dot mode
                   1 = set
                   0 = reset
*/
// BYTE os_lpt_putc(DWORD a_c);
#define os_lpt_putc(x)          sys_func1(OS_FN_LPT_PUTC,(DWORD)(x))

#define   OS_FN_LPT_PUTDW       (OS_FN_LPT_PUTC + 1)
// DWORD os_lpt_putdw(DWORD a_dw);
#define os_lpt_putdw(x)         sys_func1(OS_FN_LPT_PUTDW,(DWORD)(x))

#define   OS_FN_LPT_STATUS      (OS_FN_LPT_PUTDW + 1)
/*
  select = 0
    bit0-bit7 = printer printing status
                K_LptrOk                0
                K_ErrTempHigh           1
                K_ErrVoltLow            2
                K_ErrPaperout           3
                K_ErrPrint              4
    bit8  - temperature high
    bit9  - paper out
    bit10 - print voltage low
    bit11 - printing
    bit12 - finish
  select = 1
    restart printer if error
  select = 2
    reset printer if error
  select = 3
    reset printer
  select = 4
    read back print head temperature in ADC mV
  select = 5
    read back if printer is installed or not
*/
// DWORD os_lpt_status(DWORD a_select);
#define K_LptStatusRead         0
#define K_LptStatusRestart      1
#define K_LptStatusResetError   2
#define K_LptStatusReset        3
#define K_LptStatusTemperature  4
#define K_LptStatusInstalled    5
#define os_lpt_status(x)        sys_func1(OS_FN_LPT_STATUS,(DWORD)(x))

                /****** app routine ******/

#define   OS_FN_APP_INFO        (OS_FN_LPT_STATUS + 1)
// BOOLEAN os_app_info(DWORD a_appid,T_APP_INFO * a_pdptr);
#define os_app_info(x,y)        sys_func2(OS_FN_APP_INFO,(DWORD)(x),(DWORD)(y))

#define   OS_FN_APPCALL_RETURN  (OS_FN_APP_INFO + 1)
// void os_appcall_return(DWORD a_ret);
#define os_appcall_return(x)    sys_func1(OS_FN_APPCALL_RETURN,(DWORD)(x))

                /****** USB routine  ******/

// ----------- USB Serial Status -------------
// bit0-7: Port status, bit8-15: USB state, bit16-23: error code, bit24-31: reserved
#define K_USB_RXRDY       0x01
#define K_USB_TXRDY       0x02
#define K_USB_DISABLED    0x80
// state
#define K_USB_ATTACHED    0x0100
#define K_USB_POWERED     0x0200
#define K_USB_DEFAULT     0x0400
#define K_USB_ADDRESS     0x0800
#define K_USB_CONFIGURED  0x1000
#define K_USB_SUSPENDED   0x2000
// error code
#define K_USB_LOCKED      0x010000  // end point busy or not available
#define K_USB_ABORTED     0x020000  // transfer aborted
#define K_USB_RESET       0x030000  // end point or device reset

#define   OS_FN_USBSER_OPEN     (OS_FN_APPCALL_RETURN + 1)
// DWORD os_usbser_open()
#define os_usbser_open()        sys_func0(OS_FN_USBSER_OPEN)

#define   OS_FN_USBSER_CLOSE    (OS_FN_USBSER_OPEN + 1)
// DWORD os_usbser_close()
#define os_usbser_close()       sys_func0(OS_FN_USBSER_CLOSE)

#define   OS_FN_USBSER_READ     (OS_FN_USBSER_CLOSE + 1)
// DWORD os_usbser_read(x,y)
#define os_usbser_read(x,y)     sys_func2(OS_FN_USBSER_READ,(DWORD)(x),(DWORD)(y))

#define   OS_FN_USBSER_WRITE    (OS_FN_USBSER_READ + 1)
// DWORD os_usbser_write(x,y);
#define os_usbser_write(x,y)    sys_func2(OS_FN_USBSER_WRITE,(DWORD)(x),(DWORD)(y))

#define   OS_FN_USBSER_STATUS   (OS_FN_USBSER_WRITE + 1)
// DWORD os_usbser_status();
#define os_usbser_status()      sys_func0(OS_FN_USBSER_STATUS)

#define   OS_FN_USBSER_ENABLE  (OS_FN_USBSER_STATUS + 1)
// DWORD os_usbser_enable(x);
#define os_usbser_enable(x)     sys_func1(OS_FN_USBSER_ENABLE,(DWORD)(x))

                /****** flash routine for system use only *****/

// status return by os_fl_status
#define K_FlashFail           0x01
#define K_FlashEdcError       0x02      // for EDC status only
#define K_FlashEdcValid       0x04      // for EDC status only
#define K_FlashIdle           0x20
#define K_FlashReady          0x40

// status return by os_fl_copyback_start
#define K_FlCopybackCmdError  0
#define K_FlCopybackFail      1
#define K_FlCopybackEdcError  2
#define K_FlCopybackEdcOk     3

// flash command option
#define K_FlCopybackSupport   0x01
#define K_FlEdcSupport        0x02
#define K_FlCacheReadSupport  0x04

#define K_128MFlash           2
#define K_512MFlash           8

#define   OS_FN_FL_INFO         (OS_FN_USBSER_ENABLE + 1)
// DWORD os_fl_info(void); // system only
// return 0        = error
//        bit0-7   = size in unit of 64M
//        bit8-15  = command option
//        bit16-31 = MCode | DCcode
#define os_fl_info()            sys_func0(OS_FN_FL_INFO)

#define   OS_FN_FL_STATUS       (OS_FN_FL_INFO + 1)
// DWORD os_fl_status(void); // system only
#define os_fl_status()          sys_func0(OS_FN_FL_STATUS)

#define   OS_FN_FL_PAGE_READ    (OS_FN_FL_STATUS + 1)
// DWORD os_fl_read_page(BYTE * a_buffer,DWORD a_address,DWORD a_len); // system only
#define os_fl_read_page(x,y,z)  sys_func3(OS_FN_FL_PAGE_READ,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_PAGE         (OS_FN_FL_PAGE_READ + 1)
// DWORD os_fl_read(BYTE * a_buffer,DWORD a_offset,DWORD a_len);; // system only
#define os_fl_read(x,y,z)       sys_func3(OS_FN_FL_PAGE,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_PGM          (OS_FN_FL_PAGE + 1)
// DWORD os_fl_pgm(BYTE *a_buffer,DWORD a_address,DWORD a_len); // system only
#define os_fl_pgm(x,y,z)        sys_func3(OS_FN_FL_PGM,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_PGM_START    (OS_FN_FL_PGM + 1)
// DWORD os_fl_pgm_start(BYTE *a_buffer,DWORD a_address,DWORD a_len); // system only
#define os_fl_pgm_start(x,y,z)  sys_func3(OS_FN_FL_PGM_START,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_WRITE        (OS_FN_FL_PGM_START + 1)
// DWORD os_fl_write(BYTE * a_buffer,DWORD a_offset,DWORD a_len); // system only
#define os_fl_write(x,y,z)      sys_func3(OS_FN_FL_WRITE,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_PGM_END      (OS_FN_FL_WRITE + 1)
// DWORD os_fl_pgm_end(void); // system only
#define os_fl_pgm_end()         sys_func0(OS_FN_FL_PGM_END)

#define   OS_FN_FL_ERASE_START  (OS_FN_FL_PGM_END + 1)
// DWORD os_fl_berase_start(DWORD a_address); // system only
#define os_fl_berase_start(x)   sys_func1(OS_FN_FL_ERASE_START,(DWORD)(x))

#define   OS_FN_FL_ERASE_END    (OS_FN_FL_ERASE_START + 1)
// DWORD os_fl_berase_end(void); // system only
#define os_fl_berase_end()      sys_func0(OS_FN_FL_ERASE_END)

#define   OS_FN_FL_COPYB_START  (OS_FN_FL_ERASE_END + 1)
// DWORD os_fl_copyb_start(BYTE *a_buffer,DWORD a_src_page,DWORD a_len); // system only
#define os_fl_copyb_start(x,y,z) sys_func3(OS_FN_FL_COPYB_START,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_COPYB_PGM    (OS_FN_FL_COPYB_START + 1)
// DWORD os_fl_copyb_pgm(DWORD a_dest_page); // system only
#define os_fl_copyb_pgm(x,y,z)  sys_func1(OS_FN_FL_COPYB_PGM,(DWORD)(x))

#define   OS_FN_FL_COPYB_END    (OS_FN_FL_COPYB_PGM + 1)
// DWORD os_fl_copyb_end(BYTE *a_buffer,DWORD a_offset,DWORD a_len); // system only
#define os_fl_copyb_end(x,y,z)  sys_func3(OS_FN_FL_COPYB_END,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_CREAD_RAND   (OS_FN_FL_COPYB_END + 1)
// DWORD os_fl_cread_rand(BYTE *a_buffer,DWORD a_offset,DWORD a_len); // system only
#define os_fl_cread_rand(x,y,z) sys_func3(OS_FN_FL_CREAD_RAND,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define   OS_FN_FL_CREAD        (OS_FN_FL_CREAD_RAND + 1)
// DWORD os_fl_cread(BYTE *a_buffer,DWORD a_len); // system only
#define os_fl_cread(x,y)        sys_func2(OS_FN_FL_CREAD,(DWORD)(x),(DWORD)(y))

#define   OS_FN_FL_CREAD_END    (OS_FN_FL_CREAD + 1)
// DWORD os_fl_cread_end(BYTE *a_buffer,DWORD a_len); // system only
#define os_fl_cread_end(x,y)    sys_func2(OS_FN_FL_CREAD_END,(DWORD)(x),(DWORD)(y))

                /****** misc routine 1 ******/

#define   OS_FN_RAND            (OS_FN_FL_CREAD_END + 1)
// BOOLEAN os_rand(void * a_dest);
#define os_rand(x)              sys_func1(OS_FN_RAND,(DWORD)(x))


#define K_AppIdMask             0x000000ff
#define K_ProcIdMask            0x0000ff00
#define K_KeyAuthMask           0x00070000
#define K_SecAuthed             0x00010000      // indicates authed that security functions can be used
#define K_DllAuthed             0x00020000      // indicates authed that app dll funcitons can be used
#define K_AuthAuthed            0x00040000      // indicate authed that auth functions can be used
#define   OS_FN_GET_CALLID      (OS_FN_RAND + 1)
// DWORD os_get_caller_id(void);
#define os_get_caller_id()      sys_func0(OS_FN_GET_CALLID)

#define   OS_FN_SLEEP           (OS_FN_GET_CALLID + 1)
// void os_sleep(void);
#define os_sleep()              sys_func0(OS_FN_SLEEP)

// exit code
#define K_GprsEntryFailPpp      -14
#define K_GprsEntryFailGprsUp   -13
#define K_GprsEntryFailGprs     -12
#define K_GprsEntryFailLpt      -11
#define K_GprsEntryFailSle      -10
#define K_GprsEntryFailMmc      -9
#define K_GprsEntryFailMsr      -8
#define K_GprsEntryFailSam      -7
#define K_GprsEntryFailAux      -6
#define K_GprsEntryFailLan      -5
#define K_GprsEntryFailPower    -4
#define K_GprsEntryFailUsbS     -3
#define K_GprsSleepExitFail     -2
#define K_GprsSleepEntryFail    -1
#define K_GprsEntryFailThread   0
#define K_PowerKeyPress         1
#define K_PowerPlugIn           2
#define OS_FN_POWER_SLEEP       (OS_FN_SLEEP + 1)
 //int os_power_sleep(void);
#define os_power_sleep()        (int)sys_func0(OS_FN_POWER_SLEEP)

#define   OS_FN_RESET           (OS_FN_POWER_SLEEP + 1)
// void os_reset(void);
#define os_reset()              sys_func0(OS_FN_RESET)

#define   OS_FN_FREE_RUN        (OS_FN_RESET + 1)
// DWORD os_free_run(void);
#define os_free_run()           sys_func0(OS_FN_FREE_RUN)

              /****** Hardware Config ******/

#define K_HdModem               0x0000000F
  #define K_HdNoModem             0x00000000
  #define K_HdSi2404              0x00000001
  #define K_HdSi2415              0x00000002
  #define K_HdSi2434              0x00000003
  #define K_HdSi2457              0x00000004
#define K_HdLan                 0x00000010
  #define K_HdNoLan               0x00000000
  #define K_HdLanInstalled        0x00000010
#define K_HdPN532               0x00000020
  #define K_HdPN532Installed      0x00000020
  #define K_HdNoPN532             0x00000000
#define K_HdFlash               0x00000040
  #define K_Hd512MFlash           0x00000000
  #define K_Hd128MFlash           0x00000040
#define K_HdSdRam               0x00000080
  #define K_Hd16MSdRam            0x00000000
  #define K_Hd32MSdRam            0x00000080
#define K_HdPrinter             0x00000100
  #define K_HdPrinterInstalled    0x00000000
  #define K_HdNoPrinter           0x00000100
#define K_HdBattery             0x00000200
  #define K_HdBatteryInstalled    0x00000000
  #define K_HdNoBattery           0x00000200
#define K_HdSam                 0x00000C00
#define K_HdSamShift            10
  #define K_HdNoSam               0x00000000
  #define K_Hd1Sam                0x00000400
  #define K_Hd2Sam                0x00000800
#define K_HdIcc                 0x00001000
  #define K_HdNoIcc               0x00000000
  #define K_HdIccInstalled        0x00001000
#define K_HdMsr                 0x00006000
#define K_HdMsrShift            13
  #define K_HdNoMsr               0x00000000
  #define K_HdMsr12               0x00002000
  #define K_HdMsr23               0x00004000
  #define K_HdMsr123              0x00006000
#define K_HduSD                 0x00008000
  #define K_HdNouSD               0x00000000
  #define K_HduSDInstalled        0x00008000
#define K_HdTilt                0x00010000
  #define K_HdNoTilt              0x00000000
  #define K_HdTiltInstalled       0x00010000
#define K_HdPcb                 0xff000000        // add 1

#define   OS_FN_HD_CONFIG       (OS_FN_FREE_RUN + 1)
// DWORD os_hd_config(void);
#define os_hd_config()          sys_func0(OS_FN_HD_CONFIG)

#define K_BRegMfg0         14
#define K_BRegMfg1         15

#define   OS_FN_BREG_READ       (OS_FN_HD_CONFIG + 1)
// DWORD os_breg_mfg_read(void);    // mfg only
#define os_breg_mfg_read()      sys_func0(OS_FN_BREG_READ)

#define   OS_FN_BREG_WRITE      (OS_FN_BREG_READ + 1)
// void os_breg_mfg_write(a_value); // mfg only
#define os_breg_mfg_write(x)    sys_func1(OS_FN_BREG_WRITE,(DWORD)(x))

#define   OS_FN_RDLL_APP        (OS_FN_BREG_WRITE + 1)
//void os_rdll_app(void);
#define os_rdll_app()           sys_func0(OS_FN_RDLL_APP)

#define   OS_FN_BATT_LEVEL      (OS_FN_RDLL_APP + 1)
// Battery level in 100mV
#define K_BatteryCharging       0x80000000
#define K_PowerPluggedIn        0x40000000
// DWORD os_batt_level(void);
#define os_batt_level()         sys_func0(OS_FN_BATT_LEVEL)

#define   OS_FN_BATT_TEMP      (OS_FN_BATT_LEVEL + 1)
// int os_batt_temperature(void);
#define os_batt_temperature()   sys_func0(OS_FN_BATT_TEMP)

#define   OS_FN_OFF             (OS_FN_BATT_TEMP + 1)
// void os_off(void);
#define os_off()                sys_func0(OS_FN_OFF)

/*------------------------- GPRS Functions ---------------------------------*/

#define OS_FN_GPRS_SIGNAL       (OS_FN_OFF + 1)
// signal level 0-5
// DWORD os_gprs_signal(void);
#define os_gprs_signal()        sys_func0(OS_FN_GPRS_SIGNAL)

#define K_GsmError              0x01        // gsm initial error
#define K_GsmOn                 0x02        // gsm module turned on
#define K_GsmSleep              0x04        // gsm in sleep mode
#define K_GsmMuxTest            0x08        // cmux test string received
#define K_GsmRxReady            0x0100      // data receive ready
#define K_GsmTxReady            0x0200      // data transmit ready
#define K_GsmAtCmdReady         0x0400      // AT command send ready
#define K_GsmAtRespReady        0x0800      // AT command response ready
#define K_GsmAtReady1           0x010000    // initialized, ready to accept AT commands except photobooks, AOC, SMS
#define K_GsmAtReady2           0x020000    // ready to accept all AT commands
#define K_GsmCmuxUp             0x040000    // cmux is setup
#define K_GsmSimInserted        0x080000    // sim card inserted when K_GsmSimStatusReady
#define K_GsmNetworkAvailable   0x100000    // gsm network service available
#define K_GsmCalling            0x200000    // gsm is calling, set after ATD until no carrier
#define K_GsmGrpsUp             0x400000    // GPRS is connected, set after <CONNECT> until <NO CARRIER>
#define K_GsmSimStatusReady     0x800000    // K_GsmSimInserted status ready
#define K_GsmSmsNewMsg          0x01000000  // sms NEW message, will be cleared if read if SMS in Q is empty
#define K_GsmRi                 0x02000000  // gsm ring indicator pin, will be cleared if read
#define K_GsmSmsSendReq         0x04000000  // send sms request and will be cleared when sent to WMP100
#define K_GsmSmsSendError       0x08000000  // send sms request error, will be clear if read
#define K_GprsExist             0x80000000  // gprs exist or not

#define OS_FN_GPRS_STATUS       (OS_FN_GPRS_SIGNAL + 1)
// DWORD os_gprs_status(DWORD a_mask);
#define os_gprs_status(x)        sys_func1(OS_FN_GPRS_STATUS,(DWORD)(x))

#define OS_FN_GPRS_AT_CMD       (OS_FN_GPRS_STATUS + 1)
// DWORD os_gprs_at_cmd(BYTE *a_cmd, DWORD a_timeout);
#define os_gprs_at_cmd(x,y)     sys_func2(OS_FN_GPRS_AT_CMD,(DWORD)(x),(DWORD)(y))

#define OS_FN_GPRS_AT_RESP      (OS_FN_GPRS_AT_CMD + 1)
// DWORD os_gprs_at_resp(BYTE *a_resp,DWORD a_maxlen);
#define os_gprs_at_resp(x,y)    sys_func2(OS_FN_GPRS_AT_RESP,(DWORD)(x),(DWORD)(y))

#define OS_FN_GPRS_SEND         (OS_FN_GPRS_AT_RESP + 1)
// DWORD os_gprs_send(T_BUF_*a_ptr);  // system only
#define os_gprs_send(x)         sys_func1(OS_FN_GPRS_SEND,(DWORD)(x))

#define OS_FN_GPRS_READ         (OS_FN_GPRS_SEND + 1)
// T_BUF * os_gprs_receive(void);     // system only
#define os_gprs_receive()       (T_BUF*)sys_func0(OS_FN_GPRS_READ)

#define OS_FN_GPRS_RESET        (OS_FN_GPRS_READ + 1)
// DWORD os_gprs_reset(void);
#define os_gprs_reset()         sys_func0(OS_FN_GPRS_RESET)

#define OS_FN_GPRS_SLEEP        (OS_FN_GPRS_RESET + 1)
// DWORD os_gprs_sleep(DWORD a_enable);
#define os_gprs_sleep(x)        sys_func1(OS_FN_GPRS_SLEEP,(DWORD)(x))

                /****** gprs sms routine ******/

// SMS pdu struct
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack(1)
#endif

#define K_SmsInternational      145
#define K_SmsNational           129

typedef struct
{
  BYTE b_len;       // len = no. of phone no. digits
  BYTE b_type;      // 145 = international, 129 = national
  char s_addr[20];  // ascii code
}
#if (!__IAR_SYSTEMS_ICC__)
__attribute__((packed))
#endif
T_SMS_ADDR;

typedef struct
{
    T_SMS_ADDR  s_smsc;       // sms sevice center address
    BYTE        b_pdu_len;    // pdu length (start from tp_flags)
    BYTE        b_tp_flags;   // sms pdu first octat
    T_SMS_ADDR  s_tp_a;       // deliver:originating address, submit:destination address
    BYTE        b_tp_mr;      // message reference
    BYTE        b_tp_st;      // status
    BYTE        b_tp_fcs;     // tp-failure-cause (for RP-ERROR only)
    BYTE        b_tp_pi;      // tp-parameter indicator
    BYTE        b_tp_pid;     // protocol identifier
    BYTE        b_tp_ct;      // command type
    BYTE        b_tp_dcs;     // data coding scheme
    BYTE        s_tp_time[7]; // deliver:service center time stamp, submit:validity period
    BYTE        s_tp_dt[7];   // discharge time
    BYTE        b_tp_udl;     // user data len, max. 160 for 7-bit data, othwise max. 140
    BYTE        s_tp_ud[160]; // user data (ascii code)
}
#if (!__IAR_SYSTEMS_ICC__)
__attribute__((packed))
#endif
T_SMS_PDU;

#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

// wmode defines
#define K_WPduMode          0
#define K_WTextMode         1

// sms deliver & submit flags
#define K_TpMti             3       // (for all) Message type indicator, bit0-1
#define K_TpMms             4       // (for deliver and status report) more message to send
#define K_TpRp              0x80    // (for both) reply path
#define K_TpUdhi            0x40    // (for both) User data header indicator
#define K_TpSri             0x20    // (for deliver) status report indicator
#define K_TpRd              4       // (for submit) reject duplicates
#define K_TpVpf             0x18    // (for submit) validity peroid format
#define K_TpSrr             0x20    // (for submit) status report request
#define K_TpSrQ             0x20    // (for status report)status report qualifier

// possible values of TP_MTI        Directions
#define K_SmsDeliver        0       // receive
#define K_SmsDeliverReport  0       // send
#define K_SmsSubmit         1       // send
#define K_SmsSubmitReport   1       // receive
#define K_SmsStatusReport   2       // receive
#define K_SmsCommand        2       // send

// sms protocol identifier (tp_pid)
#define K_PidSme2Sme        0       // No interworking, sme to sme
#define K_PidImplicit       0x20    // device type is specific to this SC, or can be concluded on the basis of the address
#define K_PidTelex          0x21    // telex
#define K_PidG3Fax          0x22    // group 3 telefax
#define K_PidG4Fax          0x23    // group 4 telefax
#define K_PidVoice          0x24    // voice phone
#define K_PidErems          0x25    // european radio messaging system
#define K_PidNps            0x26    // national paging system
#define K_PidVideo          0x27    // videotex
#define K_PidTtx            0x28    // Teletex, carrier unspecified
#define K_PidTtxPspdn       0x29    // teletex in pspdn
#define K_PidTtxCspdn       0x2a    // teletex in cspdn
#define K_PidTtxPstn        0x2b    // teletex in analog pstn
#define K_pidTtxIsdn        0x2c    // teletex in isdn
#define K_PidUci            0x2d    // UCI (Universal Computer Interface, ETSI DE/PS 3 01-3)
#define K_PidMhf            0x30    // a message handling facility (known to the SC)
#define K_PidX400           0x31    // any public X.400-based message handling system
#define K_PidEmail          0x32    // Internet electronic mail
#define K_PidGsm            0x3f    // A GSM mobile station. The SC converts the SM from the received TP-Data-Coding-Scheme
                                    // to any data coding scheme supported by that MS (e.g. the default).
#define K_PidSmType0        0x40    // Short message type 0
#define K_PidSmType1        0x41    // Replace Short message type 1
#define K_PidSmType2        0x42    // Replace Short message type 2
#define K_PidSmType3        0x43    // Replace Short message type 3
#define K_PidSmType4        0x44    // Replace Short message type 4
#define K_PidSmType5        0x45    // Replace Short message type 5
#define K_PidSmType6        0x46    // Replace Short message type 6
#define K_PidSmType7        0x47    // Replace Short message type 7
#define K_PidRcm            0x5f    // return call message
#define K_PidMeDl           0x7d    // me data download
#define K_PidMeDp           0x7e    // me de-personalization short message
#define K_PidSimDl          0x7f    // sim data download

// sms data coding scheme (tp_dcs)
// for bit7-6 = 00 (GENERAL DATA CODING INDICATION)
#define K_DscTextComp       0x20    // text is compressed
#define K_DcsHaveClass      0x10    // have message class
#define K_DcsClass          3       // message class 0-3
#define K_DcsAlpha          0x0c    // alphabet (0=default 7-bit, 1=8-bit, 2=ucs2)
// for bit7-4 = 1100 (Message Waiting Indication Group: Discard Message)
// bit 7-4 = 1101 (Message Waiting Indication Group: Store Message)
// bit 7-4 = 1110 (Message Waiting Indication Group: Store Message)
#define K_DcsIndAct         8       // set indication active
#define K_DcsIndType        3       // indication type, value defined below
#define K_MwVMail           0       // value 0 = voice mail message waiting
#define K_MwFax             1       // value 1 = FAX message waiting
#define K_MwEmail           2       // value 2 = Electronic mail message waiting
#define K_MwOther           3       // value 3 = Other message waiting
//bit 7-4 = 1111 (Data coding / message class)
// bit 1-0 are message class, same as General data coding indciation

// validity period (TP_VP in relative format) defines
#define VP_MIN(x)           ((x)/5-1)        // 5min-12hour (5min interval, x=5-715)
#define VP_HALFHOUR(x)      ((x)-24+143)     // 12hour-24 (half hour interval, x=25-48)
#define VP_DAY(x)           ((x)+166)        // 2-30days (1 day interval, x=2-30)
#define VP_WEEK(x)          ((x)+192)        // 5-63 weeks (1 week interval, x=5-63)

// defines for send sms 'to' parameter
#define K_SmsDestination    'S'
#define K_SmsMemoryUnread   'W'
#define K_SmsMemoryRead     ('W'+0x100)
#define K_SmsMemoryUnsent   ('W'+0x200)
#define K_SmsMemorySent     ('W'+0x300)

// sms AckType
#define K_NoRp              0       // no reply
#define K_RpAck             1       // reply ack (with or without pdu)
#define K_RpError           2       // reply error (must with pdu)


#define OS_FN_GPRS_SMS_DECODE   (OS_FN_GPRS_SLEEP + 1)
// DWORD os_gprs_sms_decode(T_SMS_PDU *a_dest,BYTE *a_str,DWORD a_len);
#define os_gprs_sms_decode(x,y,z) sys_func3(OS_FN_GPRS_SMS_DECODE,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define OS_FN_GPRS_SMS_ACK      (OS_FN_GPRS_SMS_DECODE + 1)
// DWORD os_gprs_sms_ack(DWORD a_ack_type,T_SMS_PDU *a_pdu);
#define os_gprs_sms_ack(x,y)    sys_func2(OS_FN_GPRS_SMS_ACK,(DWORD)(x),(DWORD)(y))

#define OS_FN_GPRS_SMS_SEND     (OS_FN_GPRS_SMS_ACK + 1)
// DWORD os_gprs_sms_send(DWORD a_text_mode,T_SMS_PDU *a_msg,DWORD a_dest);
#define os_gprs_sms_send(x,y,z) sys_func3(OS_FN_GPRS_SMS_SEND,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define OS_FN_GPRS_SMS_CONTROL  (OS_FN_GPRS_SMS_SEND + 1)
// DWORD os_gprs_sms_control(DWORD a_enable);
#define os_gprs_sms_control(x)  sys_func1(OS_FN_GPRS_SMS_CONTROL,(DWORD)(x))

#define OS_FN_GPRS_SMS_READ     (OS_FN_GPRS_SMS_CONTROL + 1)
// DWORD os_gprs_sms_read(BYTE *a_buf);
#define os_gprs_sms_read(x)  sys_func1(OS_FN_GPRS_SMS_READ,(DWORD)(x))

              /****** Tilt Sensor ******/

#define OS_FN_TILT_OPEN         (OS_FN_GPRS_SMS_READ + 1)
// DWORD os_tilt_open(void);
#define os_tilt_open()          sys_func0(OS_FN_TILT_OPEN)

#define OS_FN_TILT_CLOSE        (OS_FN_TILT_OPEN + 1)
// void os_tilt_close(void);
#define os_tilt_close()         sys_func0(OS_FN_TILT_CLOSE)

#define OS_FN_TILT_READ         (OS_FN_TILT_CLOSE + 1)
// DWORD os_tilt_read(T_TILT * a_result);
#define os_tilt_read(x)         sys_func1(OS_FN_TILT_READ,(DWORD)(x))

              /****** PN532 Interface ******/
// Request Card type
#define K_TypeAIso14443_4       0x0001
#define K_TypeBIso14443_4       0x0002
#define K_MifareCard            0x0004
#define K_FelicaCard212         0x0008    // not support yet
#define K_FelicaCard424         0x0010    // not support yet

// Reponse Card type
#define K_RespTypeAIso14443_4   0x20
#define K_RespTypeBIso14443_4   0x23
#define K_RespMifareCard        0x10
#define K_RespFelicaCard212     0x11      // not support yet
#define K_RespFelicaCard424     0x12      // not support yet

#define K_PN532MaxMessLen       263
#define K_PN532MaxRespLen       (K_PN532MaxMessLen+3)

#define OS_FN_PN532_OPEN       (OS_FN_TILT_READ + 1)
// DWORD os_pn532_open(void);
#define os_pn532_open()        sys_func0(OS_FN_PN532_OPEN)

#define OS_FN_PN532_CLOSE      (OS_FN_PN532_OPEN + 1)
// DWORD os_pn532_close(void);
#define os_pn532_close()       sys_func0(OS_FN_PN532_CLOSE)

#define OS_FN_PN532_POLLCARD   (OS_FN_PN532_CLOSE + 1)
// DWORD os_pn532_poll_card(DWORD a_type, DWORD a_poll_time, DWORD a_poll_cycle);
#define os_pn532_poll_card(x,y,z) sys_func3(OS_FN_PN532_POLLCARD,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define OS_FN_PN532_SELECT     (OS_FN_PN532_POLLCARD + 1)
// DWORD os_pn532_select(DWORD a_tg);
#define os_pn532_select(x)     sys_func1(OS_FN_PN532_SELECT,(DWORD)(x))

#define OS_FN_PN532_DESELECT   (OS_FN_PN532_SELECT + 1)
// void os_pn532_deselect(DWORD a_tg);
#define os_pn532_deselect(x)   sys_func1(OS_FN_PN532_DESELECT,(DWORD)(x))

#define OS_FN_PN532_RELEASE    (OS_FN_PN532_DESELECT + 1)
// DWORD os_pn532_release(DWORD a_tg);
#define os_pn532_release(x)    sys_func1(OS_FN_PN532_RELEASE,(DWORD)(x))

#define OS_FN_PN532_PSL       (OS_FN_PN532_RELEASE + 1) // for ISO14443-4A only
// DWORD os_pn532_psl(DWORD a_tx_speed,DWORD a_rx_speed);
#define os_pn532_psl(x,y)     sys_func2(OS_FN_PN532_PSL,(DWORD)(x),(DWORD)(y))

#define OS_FN_PN532_SEND       (OS_FN_PN532_PSL + 1)
// DWORD os_pn532_send(BYTE *a_message,DWORD a_len);
#define os_pn532_send(x,y)     sys_func2(OS_FN_PN532_SEND,(DWORD)(x),(DWORD)(y))

#define OS_FN_PN532_SEND_RAW   (OS_FN_PN532_SEND + 1)
// DWORD os_pn532_send_raw(BYTE *a_ptr,DWORD a_len,DWORD a_timeout);
#define os_pn532_send_raw(x,y,z) sys_func3(OS_FN_PN532_SEND_RAW,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define OS_FN_PN532_SEND_DIRECT (OS_FN_PN532_SEND_RAW + 1)
// DWORD os_pn532_send_direct(BYTE *a_ptr,DWORD a_len,DWORD a_timeout);
#define os_pn532_send_direct(x,y,z) sys_func3(OS_FN_PN532_SEND_DIRECT,(DWORD)(x),(DWORD)(y),(DWORD)(z))

#define OS_FN_PN532_RESPONSE   (OS_FN_PN532_SEND_DIRECT + 1)
// DWORD os_pn532_response(BYTE *a_response);
#define os_pn532_response(x)   sys_func1(OS_FN_PN532_RESPONSE,(DWORD)(x))

#define OS_FN_PN532_CANCEL_CMD (OS_FN_PN532_RESPONSE + 1)
// DWORD os_pn532_cancel_cmd(void);
#define os_pn532_cancel_cmd()  sys_func0(OS_FN_PN532_CANCEL_CMD)

#define K_PN532ConfigItem0A       K_CF_PN532_CF_0A
#define K_PN532ConfigItem0B       K_CF_PN532_CF_0B
#define K_PN532ConfigItem0C       K_CF_PN532_CF_0C
#define K_PN532ConfigItem0D       K_CF_PN532_CF_0D
#define K_PN532ConfigItem02       K_CF_PN532_CF_02
#define K_PN532ConfigItem04       K_CF_PN532_CF_04
#define K_PN532ConfigItem05       K_CF_PN532_CF_05

#define OS_FN_PN532_CONFIG_SET (OS_FN_PN532_CANCEL_CMD + 1)
// DWORD os_pn532_config_set(DWORD a_cfg_item,BYTE *a_data);
#define os_pn532_config_set(x,y) sys_func2(OS_FN_PN532_CONFIG_SET,(DWORD)(x),(DWORD)(y))

#define OS_FN_PN532_CONFIG_GET (OS_FN_PN532_CONFIG_SET + 1)
// DWORD os_pn532_config_get(DWORD a_cfg_item,BYTE *a_data);
#define os_pn532_config_get(x,y) sys_func2(OS_FN_PN532_CONFIG_GET,(DWORD)(x),(DWORD)(y))

#define OS_FN_PN532_READ_REG   (OS_FN_PN532_CONFIG_GET + 1)
// DWORD os_pn532_read_reg(DWORD a_reg,BYTE * a_result);
#define os_pn532_read_reg(x,y) sys_func2(OS_FN_PN532_READ_REG,(DWORD)(x),(DWORD)(y))

#define OS_FN_PN532_WRITE_REG  (OS_FN_PN532_READ_REG + 1)
// DWORD os_pn532_write_reg(DWORD a_reg,DWORD a_value);
#define os_pn532_write_reg(x,y) sys_func2(OS_FN_PN532_WRITE_REG,(DWORD)(x),(DWORD)(y))

                /****** misc routine 2 ******/
#define OS_FN_DUMMY            (OS_FN_PN532_WRITE_REG + 1)
// void os_dummy();
#define os_dummy()             sys_func0(OS_FN_DUMMY)
                /****** USB Host routine  ******/

// ----------- USB system Status -------------
// bit 0 and bit 1: root hub status. See USB 2.0 spec Table 11-19
#define USB_ROOTHUB_LOCAL_POWER		0x0001
#define USB_ROOTHUB_OVERCURRENT		0x0002
// bit 2 to 10 host controller status. See OHCI specification 1.0a 6.2
#define USB_HC_PERIODIC_LIST_ENABLE	0x0004
#define USB_HC_ISOCHRONOUS_ENABLE	0x0008
#define USB_HC_CONTROL_LIST_ENABLE	0x0010
#define USB_HC_BULK_LIST_ENABLE		0x0020
#define USB_HC_FUNCTION_STATE		0x00c0
#define USB_HC_INTERRUPT_ROUTING	0x0100
#define USB_HC_REMOTE_WAKEUP_CONNECTED	0x0200
#define USB_HC_REMOTE_WAKEUP_ENABLE	0x0400

// bit 15 indicates that the usb host clock is turned on.
#define USB_HC_CLOCK_ON			0x8000
// bit 16-31 is the usb frame number
#define USB_HC_FRAME_NUMBER		0xffff0000

// value of USB_HC_FUNCTION_STATE
#define USB_HCFS_RESET			0x0000
#define USB_HCFS_RESUME			0x0040
#define USB_HCFS_OPERATIONAL		0x0080
#define USB_HCFS_SUSPEND		0x00c0

#define OS_FN_USBHOST_SUBMIT_BUFFER	(OS_FN_DUMMY + 1)
// int os_usbhost_submit_buffer(T_BUF *buf);	// usb host interface for device driver
#define os_usbhost_submit_buffer(x) 	(int)sys_func1(OS_FN_USBHOST_SUBMIT_BUFFER, (DWORD)x)

#define OS_FN_USBHOST_UNLINK_BUFFER     (OS_FN_USBHOST_SUBMIT_BUFFER + 1)
//int os_usbhost_unlink_buffer(T_BUF *buf);	// usb host interface for device driver
#define os_usbhost_unlink_buffer(x) 	(int)sys_func1(OS_FN_USBHOST_UNLINK_BUFFER, (DWORD)x)

#define OS_FN_USBHOST_RESET_DEVICE     	(OS_FN_USBHOST_UNLINK_BUFFER + 1)
//int os_usbhost_reset_device(T_USBH_Device *udev);	// usb host interface for device driver
#define os_usbhost_reset_device(x) 	(int)sys_func1(OS_FN_USBHOST_RESET_DEVICE, (DWORD)x)

#define OS_FN_USBHOST_STATUS     	(OS_FN_USBHOST_RESET_DEVICE + 1)
// DWORD os_usbhost_status();
#define os_usbhost_status()		sys_func0(OS_FN_USBHOST_STATUS)

#define OS_FN_USBHOST_DEVICEMAP     	(OS_FN_USBHOST_STATUS + 1)
// DWORD os_usbhost_devicemap();
#define os_usbhost_devicemap()		sys_func0(OS_FN_USBHOST_DEVICEMAP)

// ---------------- Device Interface for Device Driver ------------------

//-----------------------------------------------------------------------------
// Device related struct
//-----------------------------------------------------------------------------
struct S_Driver;
struct S_Device;

struct S_CharOperations
{
    int (*open)(struct S_Device *dev);
    void (*close)(struct S_Device *dev);
    int (*read)(struct S_Device *dev, void *data, int length);
    int (*write)(struct S_Device *dev, const void *data, int length);
    int (*flush)(struct S_Device *dev);
};

// media information - for block device
struct media_info
{
    struct S_Device *dev;       //!< block device pointer
    char vendor[16];
    char dev_name[32];
    unsigned int format;        //!< bit0-7 file system format (value), bit8: fs case sensitive
    unsigned int version;
    unsigned int phy_sect_size; //!< physical sector size (set by fs)
    unsigned int total_sectors; //!< total no. of physical sectors (set by fs)
    unsigned int flags;
    unsigned int uid;           //! user id
    void *fs_data;              //!< pointer to file system's data
};

// case-sensitive bit in format field
#define MFS_CASE_SENSITIVE      0x100

// media info flags, bit 0-3 is the lun (logical unit number)
// bit0-15 updated by driver, bit16-31 updated by file system
#define MIF_LUN_MASK            0x0f    //!< for 0-15, max. 15 luns
#define MIF_AVAILABLE           0x10    //!< indicates media is available (for removable media device)
#define MIF_VALID               0x20    //!< indicates the media is valid mounted
#define MIF_MEDIA_REMOVABLE     0x40    //!< indicates if the media is removable, such as external CDROM
#define MIF_MTD_DEVICE          0x80    //!< indicates that it is a mtd device
#define MIF_MOUNTED             0x10000

#define media_get_lun(m)    ((m)->flags & MIF_LUN_MASK)

// block operation data - used for parameter of block read/write with oob
struct blk_op_data
{
    int datalen;                //!< data length
    int ret_datalen;            //!< return data length read/written
    int ooblen;                 //!< oob length to read/write
    int ret_ooblen;             //!< return oob length read/written
    unsigned char *data;        //!< data buffer, if null, no data are read/write
    unsigned char *oob;         //!< oob buffer, if null, no oob are read/write
};

struct blk_request
{
    struct S_Device *dev;
    unsigned char *buffer;      //!< buffer for read or write
    unsigned int buf_len;       //!< buffer length
    unsigned int sector;        //!< start sector address
    unsigned int nSector;       //!< no. of sector
    unsigned int timeout;       //!< request timeout, set 0 for driver default
    unsigned char flags;        //!< bit0-3: lun, bit4: direction
    unsigned char cmd_op;       //!< scsi command operation code
    unsigned short cmd_param;    //!< command parameter
    // below params are filled by device driver
    unsigned char cmd[16];      //!< scsi command
    unsigned char sense_buffer[18];
    unsigned char cmd_len;      //!< length of cmd
    unsigned char rfu;
    unsigned int transfered;    //!< no. of byte transfered
    int result;
};

// blk_request flags
#define BLK_LUN_MASK            0x0f
// blk_request flags direction bit
#define BLK_SYSTEM_TO_DEVICE    0
#define BLK_DEVICE_TO_SYSTEM    0x10

// operation code of scsi block commands and reduced block commands
// reference: SBC-3, SPC-4, RBC, MMC-6
#define SBC_INQUIRY             0x12
#define SBC_MODE_SELECT6        0x15
#define SBC_MODE_SENSE6         0x1a
#define SBC_MODE_SELECT10       0x55
#define SBC_MODE_SENSE10        0x5a
#define SBC_READ_CAPACITY       0x25
#define SBC_READ10              0x28
#define SBC_REQUEST_SENSE       0x03
#define SBC_START_STOP_UNIT     0x1b
#define SBC_SYNCHRONIZE CACHE10 0x35
#define SBC_TEST_UNIT_READY     0x00
#define SBC_VERIFY10            0x2f
#define SBC_WRITE10             0x2a
#define SBC_WRITE_BUFFER        0x3b
#define SBC_READ_FORMAT_CAP     0x23

// result code for blk_request
#define BREQ_GOOD               0
#define BREQ_CHECK_CONDITION    1
#define BREQ_ERROR              2

//! Standard INQUIRY data format. \see SPC-4 6.4.2
struct scsi_inquiry_data
{
    unsigned char peripheral;   //!< bit5-7 qualifier, bit0-4 device type
    unsigned char rmb;          //!< bit7 RMB
    unsigned char version;
    unsigned char data_format;  //!< bit5 NormACA, bit4 HiSUP, bit0-3 response data format
    unsigned char alength;      //!< additional length
    unsigned char flags1;   //!< SCCS ACC TPGS 3PC Reserved PROTECT
    unsigned char flags2;   //!< Obsolete ENCSERV VS MULTIP Obsolete Obsolete Obsolete ADDR16
    unsigned char flags3;   //!< Obsolete Obsolete WBUS16a SYNCa Obsolete Obsolete CMDQUE VS
    char T10_VID[8];        //!< T10 VENDOR IDENTIFICATION
    char PID[16];           //!< PRODUCT IDENTIFICATION
    char revision[4];       //!< PRODUCT REVISION LEVEL
    unsigned char vendor_specific[20];
    unsigned char clocking; //!< Reserved CLOCKINGa QASa IUSa
    unsigned char reserved1;
    unsigned short descriptor[8];   //!< vendor descriptor
    unsigned char reserved2[22];
};

//! general capacity descriptor. \see MMC-6 6.23.3.2
struct scsi_capacity_desc
{
    unsigned char numBlocks[4]; //!< MSB first
    unsigned char type;         //!< descriptor type
    unsigned char blkLength[3]; //!<  block length MSB first
};

//! format capacities data. \see MMC-6 6.23.3
struct scsi_format_capacities_data
{
    unsigned char reserved[3];
    unsigned char list_length;  //!< =no. of descriptors * size of descriptor
    struct scsi_capacity_desc descriptors[1];
};

//! Fixed format sense data. \see SPC-4 4.5.3
struct scsi_sense_data
{
    unsigned char response_code;
    unsigned char obsolete;
    unsigned char flags;    //!< FILEMARK EOM ILI Reserved SENSE KEY(bit0-3)
    unsigned char info[4];
    unsigned char alength;  //!< additional sense length
    unsigned char cmd_info[4];
    unsigned char asense_code;      //!< additional sense code
    unsigned char asc_qualifier;    //!< additional sense code qualifier
    unsigned char field_code;       //!< FIELD REPLACEABLE UNIT CODE
    unsigned char key_specific[3];  //!< SENSE KEY SPECIFIC
};

struct S_BlkOperations
{
    int (*open)(struct S_Device *dev, int lun);
    void (*close)(struct S_Device *dev);
    int (*request)(struct blk_request *req);
    int (*media_changed)(struct media_info *); // check if media changed
    int (*revalidate)(struct media_info *); // revalidate media, if media changed

    // for mtd driver only
    int (*read_with_oob)(struct S_Device *dev, unsigned int sect, struct blk_op_data *);
    int (*write_with_oob)(struct S_Device *dev, unsigned int sect, struct blk_op_data *);
    int (*erase)(struct S_Device *dev, int blk_no);
    int (*is_badblock)(struct S_Device *dev, int blk_no);
    int (*mark_bad)(struct S_Device *dev, int blk_no);
    int (*write_protect)(struct S_Device *dev, int wp_on);
};

struct S_NetOperations
{
    int (*open)(struct S_Device *dev);
    void (*close)(struct S_Device *dev);
    int (*send)(struct S_Device *dev, T_BUF *buf);
    int (*pack_header)(T_BUF *buf, unsigned int type, void *src_addr, void *dest_addr);
    int (*tx_timeout)(struct S_Device *dev);
    int (*set_mac_address)(struct S_Device *dev, void *addr);
    int (*set_mru)(struct S_Device *dev);
    int (*get_mtu)(struct S_Device *dev);
};

struct S_Driver
{
    const char *name;
    unsigned int version;
    unsigned int attr;
    struct S_Bus *bus;
    struct S_Driver *next;
    int (*probe)(struct S_Device *dev);
    int (*remove)(struct S_Device *dev);
    int (*shutdown)(struct S_Device *dev);
    int (*suspend)(struct S_Device *dev);
    int (*resume)(struct S_Device *dev);
    int (*show_attr)(struct S_Device *dev, char *buf, int size);
    int (*release)(struct S_Device *dev);
    int (*ioctl)(struct S_Device *dev, unsigned int cmd, void *param);
    void *operations;
};

/**
 * driver attribute, bit0-5 are common attributes,
 * bit6,7 are device type 
 * bit8-15 are device code (same as dev_id bit8-15 in device struct)
 * bit16-23 reserved 
 * bit24-31 are driver specific attributes 
 */
#define DA_DEVICE_TYPE_MASK     0xc0
#define DA_CHAR_DRIVER          0x00
#define DA_BLOCK_DRIVER         0x40
#define DA_NET_DRIVER           0x80
#define DA_DEVICE_CODE_MASK     0xff00

#define __get_driver_type(drv)  ((drv)->attr & DA_DEVICE_TYPE_MASK)

struct S_DeviceType
{
    const char *name;
    int (*suspend)(struct S_Device *dev);
    int (*resume)(struct S_Device *dev);
    void (*release)(struct S_Device *dev);
};

struct S_Device
{
    const char *name;
    // device id, bit0-5: dev num, bit6,7: dev type, bit8-15: dev code, bit16-31: reserved
    int dev_id;
    // refence count, increase when referenced, decrease when de-referenced
    int ref_count;
    // counter for application usage. increase when app open, decrease when close
    int use_count;
    // device type
    struct S_DeviceType *type;
    // parent device, or null
    struct S_Device *parent;
    // bus pointer
    struct S_Bus *bus;
    // the driver to drive this device
    struct S_Driver *driver;
    // device's data
    void *dev_data;
    // no. of device info entry
    int nInfo;
    // device's info array (store media_info for block device)
    void *dev_info;
    // next device
    void *next;
    // release function, called by system when device release
    void (*release)(struct S_Device *dev);
};

//-----------------------------------------------------------------------------
// Device driver functions
//-----------------------------------------------------------------------------
#define OS_FN_DRIVER_REGISTER     	(OS_FN_USBHOST_DEVICEMAP + 1)
// int os_driver_register(struct S_Driver *drv, int bus_id);
#define os_driver_register(x,y) 	(int)sys_func2(OS_FN_DRIVER_REGISTER, (DWORD)x, (DWORD)y)

#define OS_FN_DRIVER_UNREGISTER     	(OS_FN_DRIVER_REGISTER + 1)
// int os_driver_unregister(struct S_Driver *drv);
#define os_driver_unregister(x) 	(int)sys_func1(OS_FN_DRIVER_UNREGISTER, (DWORD)x)

#define OS_FN_DEVICE_ALLOC	     	(OS_FN_DRIVER_UNREGISTER + 1)
// struct S_Device *os_device_alloc(unsigned long size);
#define os_device_alloc(x) 		(struct S_Device *)sys_func1(OS_FN_DEVICE_ALLOC, (DWORD)x)

#define OS_FN_DEVICE_REGISTER     	(OS_FN_DEVICE_ALLOC + 1)
// int os_device_register(struct S_Device *dev, int bus_id);
#define os_device_register(x,y) 	(int)sys_func2(OS_FN_DEVICE_REGISTER, (DWORD)x, (DWORD)y)

#define OS_FN_DEVICE_UNREGISTER     	(OS_FN_DEVICE_REGISTER + 1)
// int os_device_unregister(struct S_Device *dev);
#define os_device_unregister(x) 	(int)sys_func1(OS_FN_DEVICE_UNREGISTER, (DWORD)x)

#define OS_FN_INTERRUPT_REGISTER     	(OS_FN_DEVICE_UNREGISTER + 1)
// int os_interrupt_register(int intr_id, int (*intr)());
#define os_interrupt_register(x,y) 	(int)sys_func2(OS_FN_INTERRUPT_REGISTER, (DWORD)x, (DWORD)y)

#define OS_FN_INTERRUPT_UNREGISTER     	(OS_FN_INTERRUPT_REGISTER + 1)
// int os_interrupt_unregister(int intr_id, int (*intr)());
#define os_interrupt_unregister(x,y) 	(int)sys_func2(OS_FN_INTERRUPT_UNREGISTER, (DWORD)x, (DWORD)y)

#define OS_FN_DEVICE_ALLOC_MEDIA     	(OS_FN_INTERRUPT_UNREGISTER + 1)
// int os_device_alloc_media(struct S_Device *dev, int nMedia);
#define os_device_alloc_media(x,y) 	(int)sys_func2(OS_FN_DEVICE_ALLOC_MEDIA, (DWORD)x, (DWORD)y)

// ----------- Device Interface for Application -------------

//------------------------------------------------------------------------------
// ioctl commands
//------------------------------------------------------------------------------
/**
 * ioctl command format
 * bit31-24: device code, bit23-16: command code, bit15-14: direction, bit13-0: param size
 * if device code == 0, it is a common device command, and will be handled in devdrv.c
 */
#define IOCMD_DIR_READ          0x8000
#define IOCMD_DIR_WRITE         0x4000

/*!
 * get device info
 * param = buffer
 * return error code
 * output: param = T_DeviceInfo 
 */
#define IOCMD_GET_DEVINFO   ((1<<16) | IOCMD_DIR_READ | sizeof(T_DeviceInfo))

/*!
 * suspend device
 * param = 0
 * return error code
 */
#define IOCMD_DEVICE_SUSPEND   ((2<<16) | 0 | 0)

/*!
 * resume device
 * param = 0
 * return error code
 */
#define IOCMD_DEVICE_RESUME   ((3<<16) | 0 | 0)

/*!
 * get block device's total number of logical unit
 * param = 0
 * return number of logical unit or error code.
 */
#define IOCMD_BLK_TOTAL_LUN ((4<<16) | IOCMD_DIR_READ | 0)

/*!
 * get media's capacity and sector size
 * param = unsigned int [3], where param[0] = logical unit number (lun)
 * return error code.
 * output: param[1] = total no. of block in device, param[2] = block size in byte
 */
#define IOCMD_MEDIA_CAPACITY    ((5<<16) | IOCMD_DIR_READ | 12)

/*!
 * read a block from device
 * param = block address (4 bytes, little endian) + 3 bytes reserved + 1 byte lun + buffer[block size]
 * return number of bytes read or error code
 */
#define IOCMD_MEDIA_BLK_READ    ((6<<16) | IOCMD_DIR_READ | 512 + 8)

/*!
 * write a block to device
 * param = block address (4 bytes, little endian) + 3 bytes reserved + 1 byte lun + buffer[block size]
 * return number of bytes written or error code
 */
#define IOCMD_MEDIA_BLK_WRITE  ((7<<16) | IOCMD_DIR_WRITE | 512 + 8)

/*!
 * get comm port status
 * param = 0
 * return status or error code
 */
#define IOCMD_COMM_STATUS   (((DEV_COMM_PORT & 0xff00)<<16) | (1<<16) | IOCMD_DIR_READ)

// usbh serial state
#define K_SERIAL_RXRDY          1
#define K_SERIAL_TXRDY          2
#define K_SERIAL_DISABLED       0x80

// device id (bit8-15:dev code, bit6-7:dev type)
// char device (dev code from 0x01-0x7f inclusive)
#define DEV_GENERIC_USB 0x0100
#define DEV_COMM_PORT   0x0200
#define DEV_BUZZER      0x0300
#define DEV_SPEAKER     0x0400
#define DEV_KEYBOARD    0x0500
#define DEV_THERMAL_PRN 0x0600
#define DEV_IMPACT_PRN  0x0700
#define DEV_USBHUB      0x0800
#define DEV_MODEM       0x0900
#define DEV_ICCARD      0x0a00
#define DEV_SAM         0x0b00
#define DEV_CONTACTLESS 0x0c00
#define DEV_MSR         0x0d00
#define DEV_BATTERY     0x0e00
// block device (dev code from 0x80-0xbf inclusive)
#define DEV_USB_STORAGE 0x8040
#define DEV_SD_CARD     0x8140
// network device (dev code from 0xc0-0xff inclusive)
#define DEV_ETHERNET    0xc080
#define DEV_PPP         0xc180

/*!
 * set comm port config
 * param = buffer for comm port config
 * return status or error code
 */
#define IOCMD_COMM_SET_CONFIG (((DEV_COMM_PORT & 0xff00)<<16) | (3<<16) | IOCMD_DIR_WRITE | 512)


// define bus id
enum
{
    BUS_ID_SYSTEM,
    BUS_ID_USB,
    MAX_BUSES
};

typedef struct S_DeviceInfo
{
    char name[64];
    int ref_count;
    unsigned int dev_id;
    unsigned int bus_id;
    char bus_name[64];
    char drv_name[64];
    unsigned int drv_version;
    unsigned int drv_attr;
    char dev_data[1024];	//!< string in format <item1>:<value1>\n<item2>:<value2>\n...
} T_DeviceInfo;

typedef struct S_DevIterator
{
    unsigned int filter;    //!< bit31-24:bus id, bit15-6:device
    unsigned int filter_mask;
    int bus_id;
    int dev_index;
} T_DevIterator;

// filter mask defines
#define DI_FILTER_BUS   0xff000000
#define DI_FILTER_DEV 	0xffc0

#define OS_FN_BUS_START		     	(OS_FN_DEVICE_ALLOC_MEDIA + 1)
// int os_bus_start(int bus_id);
#define os_bus_start(x) 		(int)sys_func1(OS_FN_BUS_START, (DWORD)x)

#define OS_FN_BUS_STOP		     	(OS_FN_BUS_START + 1)
// int os_bus_stop(int bus_id);
#define os_bus_stop(x) 			(int)sys_func1(OS_FN_BUS_STOP, (DWORD)x)

#define OS_FN_DEVICE_IOCTL		(OS_FN_BUS_STOP + 1)
// int os_device_ioctl(int dev_id, unsigned int cmd, void *param)
#define os_device_ioctl(x,y,z)		(int)sys_func3(OS_FN_DEVICE_IOCTL, (DWORD)x, (DWORD)y, (DWORD)z)

#define OS_FN_DEVICE_SEARCH_INIT	(OS_FN_DEVICE_IOCTL + 1)
// void os_device_search_init(T_DevIterator *dit, unsigned int filter, unsigned int mask)
#define os_device_search_init(x,y,z)	sys_func3(OS_FN_DEVICE_SEARCH_INIT, (DWORD)x, (DWORD)y, (DWORD)z)

#define OS_FN_DEVICE_SEARCH_NEXT	(OS_FN_DEVICE_SEARCH_INIT + 1)
// int os_device_search_next(T_DevIterator *dit, T_DeviceInfo *dev_info)
#define os_device_search_next(x,y)	(int)sys_func2(OS_FN_DEVICE_SEARCH_NEXT, (DWORD)x, (DWORD)y)

#define OS_FN_DEVICE_OPEN		(OS_FN_DEVICE_SEARCH_NEXT + 1)
// int os_device_open(int dev_id, unsigned int mode)
#define os_device_open(x,y)		(int)sys_func2(OS_FN_DEVICE_OPEN, (DWORD)x,(DWORD)y)

#define OS_FN_DEVICE_MOUNT		(OS_FN_DEVICE_OPEN + 1)
// int os_device_mount(int dev_id, int lun, const char *mount_point)
#define os_device_mount(x,y,z)		(int)sys_func3(OS_FN_DEVICE_MOUNT, (DWORD)x, (DWORD)y, (DWORD)z)

#define OS_FN_DEVICE_UNMOUNT		(OS_FN_DEVICE_MOUNT + 1)
// int os_device_unmount(const char *mount_point, int force)
#define os_device_unmount(x,y)		(int)sys_func2(OS_FN_DEVICE_UNMOUNT, (DWORD)x, (DWORD)y)

#define OS_FN_FILE_IOCTL		(OS_FN_DEVICE_UNMOUNT + 1)
// int os_file_ioctl(DWORD a_fid, unsigned int cmd, void *param)
#define os_file_ioctl(x,y,z)		(int)sys_func3(OS_FN_FILE_IOCTL, (DWORD)x, (DWORD)y, (DWORD)z)

enum appdl_result_list
{
	K_APPDL_RESULT_OKAY = 0,
	K_APPDL_RESULT_SAME = 1,
	K_APPDL_RESULT_INVALID_HDR = (DWORD)-5,
	K_APPDL_RESULT_CS_ERROR = (DWORD)-4,
	K_APPDL_RESULT_AUTHERR = (DWORD)-3,
	K_APPDL_RESULT_DATAERR = (DWORD)-2,
	K_APPDL_RESULT_UNKNOWN = (DWORD)-1
};

#define OS_FN_APPDL_WRITE_APP    (OS_FN_FILE_IOCTL + 1)
// DWORD os_appdl_write_app(const char *ap_filename)
#define os_appdl_write_app(x) (DWORD)sys_func1(OS_FN_APPDL_WRITE_APP, (DWORD)x)

                /****** ADC rountine ******/

#define OS_FN_ADC_CH_ENABLE  (OS_FN_APPDL_WRITE_APP + 1)
// void os_adc_ch_enable(DWORD a_which,DWORD a_sample_time); // system only, a_sample_time in 10ms unit
#define os_adc_ch_enable(x,y)  sys_func2(OS_FN_ADC_CH_ENABLE,(DWORD)(x),(DWORD)(y))

#define OS_FN_ADC_CH_DISABLE (OS_FN_ADC_CH_ENABLE + 1)
// void os_adc_ch_disable(DWORD a_which); // system only
#define os_adc_ch_disable(x)   sys_func1(OS_FN_ADC_CH_DISABLE,(DWORD)(x))

#define OS_FN_ADC_CH_READ    (OS_FN_ADC_CH_DISABLE + 1)
// DWORD os_adc_ch_read(DWORD a_which); // system only, if channel not enable, return 0
#define os_adc_ch_read(x)      sys_func1(OS_FN_ADC_CH_READ,(DWORD)(x))

              /****** mdm routine ******/

#define K_AsyncMode             0x00
#define K_SyncMode              0x04
#define K_CcittMode             0x00
#define K_BellMode              0x02
#define K_BlindDialing          0x08
#define K_SoftReset             0x10
#define K_HardReset             0x00
#define K_ShortDialtone         0x20
#define K_NormalDialtone        0x00
#define K_NoLoopCurrentDetect   0x40
#define K_LoopCurrentDetect     0x00
#define K_DetectBusyTone        0x00
#define K_DisableBusyTone       0x01

#define K_DiscNormal            0x00
#define K_DiscNReset            0x01
#define K_DiscRedial            0x02
#define K_DiscTestMode          0x03

#define OS_FN_MDM_OPEN          (OS_FN_ADC_CH_READ + 1)
//BOOLEAN os_mdm_open(void * a_init_str);
#define os_mdm_open(x)          sys_func1(OS_FN_MDM_OPEN,(DWORD)(x))

#define OS_FN_MDM_CLOSE         (OS_FN_MDM_OPEN + 1)
//void os_mdm_close(void);
#define os_mdm_close()          sys_func0(OS_FN_MDM_CLOSE)

#define OS_FN_MDM_DIAL          (OS_FN_MDM_CLOSE + 1)
//BOOLEAN os_mdm_dial(void * a_dial_str);
#define os_mdm_dial(x)          sys_func1(OS_FN_MDM_DIAL,(DWORD)(x))

#define OS_FN_MDM_DISC          (OS_FN_MDM_DIAL + 1)
//void os_mdm_disc(DWORD a_test_mode);
#define os_mdm_disc(x)           sys_func1(OS_FN_MDM_DISC,(x))

#define OS_FN_MDM_CID           (OS_FN_MDM_DISC + 1)
//DWORD os_mdm_cid(void * a_ptr);
#define os_mdm_cid(x)           sys_func1(OS_FN_MDM_CID,(DWORD)(x))

#define OS_FN_MDM_STATUS        (OS_FN_MDM_CID + 1)
//DWORD os_mdm_status(DWORD a_select);
#define os_mdm_status(x)        sys_func1(OS_FN_MDM_STATUS,(x))

#define K_MdmStatus       0
#define K_MdmReady              0
#define K_MdmConnected          1
#define K_MdmRingIn             2
#define K_MdmNoCarrier          3
#define K_MdmNoDialTone         4
#define K_MdmLineInUse          5
#define K_MdmNoLine             6
#define K_MdmBusy               7
#define K_MdmIntrusion          8
#define K_MdmDialing            9
#define K_MdmToVoice            10
#define K_MdmCid                11


#define K_MdmSpeed        1
#define K_Speed300              0
#define K_Speed1200             1
#define K_Speed2400             2
#define K_Speed4800             3
#define K_Speed7200             4
#define K_Speed9600             5
#define K_Speed12000            6
#define K_Speed14400            7
#define K_Speed16800            8
#define K_Speed19200            9
#define K_Speed21600            10
#define K_Speed24000            11
#define K_Speed26400            12
#define K_Speed28000            13
#define K_Speed28800            14
#define K_Speed29333            15
#define K_Speed30666            16
#define K_Speed31200            17
#define K_Speed32000            18
#define K_Speed33333            19
#define K_Speed33600            20
#define K_Speed34000            21
#define K_Speed34666            22
#define K_Speed36000            23
#define K_Speed37333            24
#define K_Speed38000            25
#define K_Speed38666            26
#define K_Speed40000            27
#define K_Speed41333            28
#define K_Speed42000            29
#define K_Speed42666            30
#define K_Speed44000            31
#define K_Speed45333            32
#define K_Speed46000            33
#define K_Speed46666            34
#define K_Speed48000            35
#define K_Speed49333            36
#define K_Speed50000            37
#define K_Speed50666            38
#define K_Speed52000            39
#define K_Speed53333            40
#define K_Speed54000            41
#define K_Speed54666            42
#define K_Speed56000            43

#define K_MdmOnline       2   // 1 = online, 0 = offline
#define K_MdmHsInUse      3   // 0 = free, 1 = in use, 2 = no line
// ** n.b poll line free or phone off hook must be done in period > 1 sec **

#define K_MdmOnHook       4
#define K_MdmOffHook      5

#define K_MdmModel        6
#define K_ScxV32bis             0
#define K_ScxV34                1
#define K_ScxV92                2

#define K_MdmCidEnable    7
#define K_MdmCidDisable   8
#define K_MdmCidDetected  9

#define K_MdmDisconnected 10
#define K_MdmDialFinish   11  // 0 = disconnect finish, other = disconnecting

#define K_MdmCmdMode      12  // switch from data mode to command by sending +++ and wait for OK
#define K_MdmDataMode     13  // switch from command mode back to data mode
#define K_MdmSignalLevel  14  // get online signal level
#define K_MdmSignalQual   15  // get online signal quality
#define K_MdmLastStatus   16  // get last connection status
#define K_MdmTpVolt       17  // return in unit of hundredth volt, in command mode. In Online, return 0
                              // if dialing, return 10V
                              // < 3V => no line
                              // > 3V && <15V => line in use
                              // >= 15V => line free

#define K_MdmCertTest     18

#define K_MdmReadSReg     0x80000000  //0x80NN0000,  where NN is the SReg no
#define K_MdmSetSReg      0x40000000  //0x40NN00VV, wherr NN is the SReg no and VV is the value

                /****** mcom rountine ******/

#define K_ModeVisa1      0
#define K_ModeSdlc       1
#define K_ModeAsync      2

#define OS_FN_MCOM_OPEN         (OS_FN_MDM_STATUS + 1)
//BOOLEAN os_mcom_open(void * init_str);
#define os_mcom_open(x)         sys_func1(OS_FN_MCOM_OPEN,(DWORD)(x))

#define OS_FN_MCOM_CLOSE        (OS_FN_MCOM_OPEN + 1)
//BOOLEAN os_mcom_close(void);
#define os_mcom_close()         sys_func0(OS_FN_MCOM_CLOSE)

#define OS_FN_MCOM_PERMIT       (OS_FN_MCOM_CLOSE + 1)
//BOOLEAN os_mcom_permit(void);
#define os_mcom_permit()        sys_func0(OS_FN_MCOM_PERMIT)

#define OS_FN_MCOM_RXRDY        (OS_FN_MCOM_PERMIT + 1)
//BOOLEAN os_mcom_rxrdy();
#define os_mcom_rxrdy()         sys_func0(OS_FN_MCOM_RXRDY)

#define OS_FN_MCOM_TXRDY        (OS_FN_MCOM_RXRDY + 1)
//BOOLEAN os_mcom_txrdy();
#define os_mcom_txrdy()         sys_func0(OS_FN_MCOM_TXRDY)

#define OS_FN_MCOM_SREAD        (OS_FN_MCOM_TXRDY + 1)
//T_BUF * os_mcom_sread(void); // system only
#define os_mcom_sread()         ((T_BUF *)sys_func0(OS_FN_MCOM_SREAD))

#define OS_FN_MCOM_SSEND        (OS_FN_MCOM_SREAD + 1)
//BOOLEAN os_mcom_ssend(void * ptr); // system only
#define os_mcom_ssend(x)        sys_func1(OS_FN_MCOM_SSEND,(DWORD)(x))

// VISA 1
#define K_RxTimeout             1
#define K_RspTimeout            2
#define K_ConnectFail           3
#define K_LinkDrop              4
// SDLC
#define K_DiscRcvd              1               /* Disconnect received */
#define K_NoPolling             2               /* No SDLC polling */
#define K_RetryErr              3               /* Retry exceeded */
// ASYNC
#define K_Overflow              1

#define OS_FN_MCOM_STATUS       (OS_FN_MCOM_SSEND + 1)
//DWORD os_mcom_status(DWORD select);
#define os_mcom_status(x)       sys_func1(OS_FN_MCOM_STATUS,(x))

#define OS_FN_MCOM_READ         (OS_FN_MCOM_STATUS + 1)
//DWORD os_mcom_read(BYTE * a_buf); // app only
#define os_mcom_read(x)         sys_func1(OS_FN_MCOM_READ,(DWORD)(x))

#define OS_FN_MCOM_SEND         (OS_FN_MCOM_READ + 1)
//BOOLEAN os_mcom_send(BYTE * a_buf,DWORD a_len); // app only
#define os_mcom_send(x,y)       sys_func2(OS_FN_MCOM_SEND,(DWORD)(x),(DWORD)(y))

#endif

